Alex Rivera | Logout

PHP 5: const vs static

Asked 2009-11-06T07:10:18.107
194

In PHP 5, what is the difference between using const and static?

When is each appropriate? And what role does public, protected and private play - if any?

Edit
Report

1 Answer

20

One last point that should be made is that a const is always static and public. This means that you can access the const from within the class like so:

class MyClass
{
     const MYCONST = true;
     public function test()
     {
          echo self::MYCONST;
     }
}

From outside the class you would access it like this:

echo MyClass::MYCONST;
answered 2010-03-04T01:57:45.590

Your Answer