PHP 5: const vs static

前端 未结 7 855
长情又很酷
长情又很酷 2020-11-29 16:38

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

When is each appropriate? And what role does public, pr

7条回答
  •  感情败类
    2020-11-29 17:11

    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;
    

提交回复
热议问题