Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use an static var from child class in its parent class with an static method

I want to get the value of the static var that was redeclared in the subclass :

    class A {

        private static $echo_var = 'PARENT_ECHO' ;

        public static function e() {
            return '$echo_var = ' . self::$echo_var ;
        }
    }

    class B extends A {

        private static $echo_var = 'CHILD_ECHO';
    }

    echo B::e();

I want to get CHILD_ECHO.

thanks, Mottenmann

like image 991
Mottenmann Avatar asked Jan 26 '26 13:01

Mottenmann


1 Answers

Use the static keyword when accesing it:

return '$echo_var = ' . static::$echo_var ;

It's called late static binding. But it won't work on private members. You'll have to make it public or protected. Private properties are accessible only in the class in which they are defined.

like image 114
nice ass Avatar answered Jan 29 '26 03:01

nice ass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!