How to access constant defined in child class from parent class functions?

后端 未结 5 1679
时光说笑
时光说笑 2021-02-03 18:37

I saw this example from php.net:



        
5条回答
  •  忘掉有多难
    2021-02-03 18:51

    I couldn't get it to work with const as it prints "yonderyonder" (that's the thing about constants, they don't change), but it works fine with var:

    MY_CONST;
         }
    }
    
    class ChildClass extends MyClass {
    
         var $MY_CONST = "bar";
    }
    
    $x = new ChildClass(); // prints 'bar'
    $y = new MyClass(); // prints 'yonder'
    
    ?>
    

提交回复
热议问题