Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Why am I getting errors about static properties?
    primarykey
    data
    text
    <p><a href="http://codepad.viper-7.com/I0Zqoi" rel="nofollow">http://codepad.viper-7.com/I0Zqoi</a></p> <p>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.</p> <pre><code>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 </code></pre> <p>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. :(((</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload