http://codepad.viper-7.com/I0Zqoi
I don’t understand what’s wrong with this or how to fix it or why. Can someone who knows a little about programming please explain what’s happening behind the scenes, like on the interpreter level? Also, how can I fix my problem, and why do I need to write my code in the way of the correction? Can you tell me, in human language, what is wrong with this and how to make it better? I guess my book isn’t explaining well, and the code inside of it doesn’t work. :/ Thank you.
class A
{
private $foo = 'bar';
function read()
{
echo self::$foo;
}
}
$a = new A();
a::read();
Strict Standards: Non-static method A::read() should not be called statically in /code/I0Zqoi on line 13
Fatal error: Access to undeclared static property: A::$foo in /code/I0Zqoi on line 8
The only workaround seems to be to add “static” in front of the method. Apparently, non-static methods cannot be accessed by static means (e.g., class A{function read(){echo “whatever”};} cannot be accessed by a::read() because the -> operator is necessary). Also, static properties cannot be accessed by object code, even if they exist within a function (e.g., class A{static $variable; function read(){echo $this->variable};} a->read(); won’t work because the -> operator is being used to access a function that calls a static property.). By changing both the method and the property to static, the method can be accessed by static means. By changing both the method and property to non-static makes it so that either can be accessed with object instanciation. It doesn’t make sense to me that the debugger is throwing errors because my book says that static properties can be called from non-static methods via object code calls to the non-static methods. So, is the debugger broken? Because I’ve tried every combination, and the code only seems to work if both the method and property are either static or non-static. :(((
The Strict Standards part is because
a::read()is being called in a static context with::. After declaring$aas a class instance ofA, you should call theread()method on the variable using the->operator:In the class definition,
$foois declared as a private property, but without thestatickeyword. It is then referred to in static context using the::operator instead of the->. The proper way to access it would beLNow what does
staticmean anyway? Static methods and properties refer to class methods and properties that are shared by all current and future class instances. IfA::$foohad been declared as:then there would be only the one
$foofor all of classAin your code. Changing$foowould affect all instances of classA, and$foocan be accessed without even creating an instance of the class (likenew A();)Likewise, static methods can be called without creating an instance of the class because they do not modify class properties that are not also static.
To declare properties and methods as
static, just add thestatickeyword:EDIT for more examples:
Now see it in action:
I stuffed this whole thing into the codepad as well: http://codepad.viper-7.com/tV6omK
The book you’re reading must not be paying attention to strict standards. If a non-static function does not attempt to access/modify a non-static property, you can call it statically successfully, but it WILL issue a strict warning. If the non-static function does modify or access a non-static property with
$this->property, it will be a fatal error. You can’t do that.In PHP’s
error_reporting, the setting ofE_ALLfor show all errors actually does not include strict warnings. That has to be done withE_ALL & E_STRICT.