Is there a way to have PHP subclasses inherit properties (both static and instance)?

前端 未结 2 2014
予麋鹿
予麋鹿 2021-01-19 08:26

If I declare a base class as follows:

abstract class Parent {

  protected static $message = \"UNTOUCHED\";

     public static function yeah() {
         st         


        
相关标签:
2条回答
  • 2021-01-19 08:45

    Inheritance and static properties does sometime lead to "strange" things in PHP.

    You should have a look at Late Static Bindings in the PHP's manual : it explains what can happen when inheriting and using static properties in PHP <= 5.2 ; and gives a solutions for PHP >= 5.3 where you can use the static:: keywords instead of self::, so that static binding is done at execution (and not compile) time.

    0 讨论(0)
  • 2021-01-19 08:46

    The answer is "with a workaround".

    You have to create a static constructor and have it called to copy the property.

    0 讨论(0)
提交回复
热议问题