note 86179 added to language.oop5.constants
| From: | cwreace at yahoo dot com | Date: | Tue, 07 Oct 2008 01:22:11 +0000 |
| Subject: | note 86179 added to language.oop5.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145111@lists.php.net to get a copy of this message | ||
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