简体   繁体   中英

Accessing child property from Parent class in php

I come from java background and recently testing some OOP methodology in PHP. I found that it is permitted for a parent class to use Child class property. The code below describes what I am trying to say.

<?php
    class ParentClass {
        public static function test() {
            echo "hello world ". implode(',', static::$prop);
        }
    }

    class ChildClass extends ParentClass {
        public static $prop = ['a' , 'b'];
    }

ChildClass::test();

?>

On the above code, the ParentClass is using the $prop variable from its function test() . This simply outputs with no error. However, it is not permissible in Java. What is the reason behind this methodology in PHP?

Java doesn't have the concept of Late static bindings :

"Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information.

If you use self:: you'll have the behaviour as you know it from Java. However static:: resolves at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM