Discussion:
Reading static variables in child-classes failing?
Simon Schick
2013-12-16 11:36:52 UTC
Permalink
Hi, all

I'm currently testing something in PHP with static variables ...

Do you know, why I can't read static variables of a child class, even so
the variable is protected or public?

Here's an example:

class A {
protected static $foo = "bar";
}

class B {
public function readFoo() {
return self::$foo;
}
}

When now calling B::readFoo, I get the following error:

var_dump((new B())->readFoo());

PHP Fatal error: Access to undeclared static property: B::$foo in php
shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
PHP 2. B->readFoo() php shell code:1

Fatal error: Access to undeclared static property: B::$foo in php shell
code on line 1

Call Stack:
86.2643 231224 1. {main}() php shell code:0
86.2666 231352 2. B->readFoo() php shell code:1

Tested using PHP 5.5.6

Bye,
Simon
Simon Schick
2013-12-16 11:50:44 UTC
Permalink
Please drop what I wrote here .... works! I still had some logic-failures
in here - specially in the sample-code ....

Bye,
Simon
Post by Simon Schick
Hi, all
I'm currently testing something in PHP with static variables ...
Do you know, why I can't read static variables of a child class, even so
the variable is protected or public?
class A {
protected static $foo = "bar";
}
class B {
public function readFoo() {
return self::$foo;
}
}
var_dump((new B())->readFoo());
PHP Fatal error: Access to undeclared static property: B::$foo in php
shell code on line 1
PHP 1. {main}() php shell code:0
PHP 2. B->readFoo() php shell code:1
Fatal error: Access to undeclared static property: B::$foo in php shell
code on line 1
86.2643 231224 1. {main}() php shell code:0
86.2666 231352 2. B->readFoo() php shell code:1
Tested using PHP 5.5.6
Bye,
Simon
m***@behnke.biz
2013-12-16 12:40:37 UTC
Permalink
Post by Simon Schick
class A {
   protected static $foo = "bar";
}
class B {
   public function readFoo() {
     return self::$foo;
   }
}
You forgot to extend from A
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...