LMPX.COM |
Home | Linux | Mysql | PHP | XML | ||
|
|
|||
From: cwreace@yahoo.com Date: Mon Oct 6 19:22:11 2008 Subject: note 86179 added to language.oop5.constants
A useful technique I've found is to use interfaces for package- or application-wide constants, making it easy to incorporate them into any classes that need access to them:
<?php
interface AppConstants
{
const FOOBAR = 'Hello, World.';
}
class Example implements AppConstants
{
public function test()
{
echo self::FOOBAR;
}
}
$obj = new Example();
$obj->test(); // outputs "Hello, world."
?>
I realize the same could be done simply by defining the constant in a class and accessing it via "class_name::const_name", but I find this a little nicer in that the class declaration makes it immediately obvious that you accessing values from the implemented interface.
----
Server IP: 208.69.120.35
Probable Submitter: 70.104.68.128
----
Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php
Edit -- https://master.php.net/note/edit/86179
Del: integrated -- https://master.php.net/note/delete/86179/integrated
Del: useless -- https://master.php.net/note/delete/86179/useless
Del: bad code -- https://master.php.net/note/delete/86179/bad+code
Del: spam -- https://master.php.net/note/delete/86179/spam
Del: non-english -- https://master.php.net/note/delete/86179/non-english
Del: in docs -- https://master.php.net/note/delete/86179/in+docs
Del: other reasons-- https://master.php.net/note/delete/86179
Reject -- https://master.php.net/note/reject/86179
Search -- https://master.php.net/manage/user-notes.php
| Navigate in group php.notes at sever news.php.net | |
| Previous | Next |
| © No Copyright You are free to use Anything |
Site Maintained by PHP Developer
Powered By PHP Consultants |