note 86179 deleted from language.oop5.constants by cmb
| From: | cmb@php.net | Date: | Wed, 31 Aug 2016 17:17:08 +0000 |
| Subject: | note 86179 deleted from language.oop5.constants by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206442@lists.php.net to get a copy of this message | ||
Note Submitter: cwreace at yahoo dot com
----
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.