note 86179 added to language.oop5.constants

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.