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


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.

There are 3 errors:

  • ECHO is a reserved name.
  • Use protected instead of private
  • Use static instead of self
class A

    protected static $echo_var = 'PARENT_ECHO' ;

    public static function output()
        return '$echo_var = ' . static::$echo_var ;

class B extends A
    protected static $echo_var = 'CHILD_ECHO';

echo B::output();

Need Your Help

Forcing certain compiler-generated variables into specific ELF sections (with gcc)

c gcc linker elf

I'll start with the ultimate question: In C with gcc, is it possible to get the value(s) of __func__ (or equivalently, __FUNCTION__) stored in a section other than .rodata (or wherever -mrodata= po...

How to insert a document with date in mongo?

java eclipse mongodb

We are trying to insert a document with the current date as it's field. We are writing in java using eclipse plugin for mongodb. We want to execute the 'Date()' command of mongo to get the date from

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.