Calling Static Method from Class B(which extends Class A) of Class A

后端 未结 5 1650
遇见更好的自我
遇见更好的自我 2021-01-03 04:02

There was an interesting question in a practice test that I did not understand the answer to. What is the output of the following code:



        
5条回答
  •  暖寄归人
    2021-01-03 04:29

    Foo::getName() is using an older PHP4 style of scope resolution operator to allow an overridden method to be called.

    In PHP5 you would use parent::getName() instead

    It's useful if you want to extend, rather than completely override the behaviour of the base class, e.g. this might make it clearer

    class Bar extends Foo {
        public $name = 'John';
    
        public function getName() {
            echo "My name is ";
            parent::getName();
        }
    }
    

提交回复
热议问题