PHP 5: const vs static

Viewed 97963

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?

7 Answers

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;
Related