Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Strict Standards part is because <code>a::read()</code> is being called in a static context with <code>::</code>. After declaring <code>$a</code> as a class instance of <code>A</code>, you should call the <code>read()</code> method on the variable using the <code>-&gt;</code> operator:</p> <pre><code>// Proper non-static method call $a = new A(); $a-&gt;read(); </code></pre> <p>In the class definition, <code>$foo</code> is declared as a private property, but without the <code>static</code> keyword. It is then referred to in static context using the <code>::</code> operator instead of the <code>-&gt;</code>. The proper way to access it would beL</p> <pre><code>// Proper reference to non-static $foo function read() { echo $this-&gt;foo; } </code></pre> <p>Now what does <code>static</code> mean anyway? Static methods and properties refer to class methods and properties that are shared by all current and future class instances. If <code>A::$foo</code> had been declared as:</p> <pre><code>private static $foo; </code></pre> <p>then there would be only the one <code>$foo</code> for all of class <code>A</code> in your code. Changing <code>$foo</code> would affect all instances of class <code>A</code>, and <code>$foo</code> can be accessed without even creating an instance of the class (like <code>new A();</code>)</p> <p>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.</p> <pre><code>// Static method call: A::read(); </code></pre> <p>To declare properties and methods as <code>static</code>, just add the <code>static</code> keyword:</p> <pre><code>// Property private static $foo; // Method public static function foo() {} </code></pre> <p><strong>EDIT for more examples</strong>:</p> <pre><code>class A { // Private property (non-static) private $foo; // Public property (static) public static $bar = 12345; // Public (non-static) function to access private $foo public function getFoo() { return $this-&gt;foo; } // Public (non-static) function to set private $foo public function setFoo($newfoo) { $this-&gt;foo = $newfoo; } // Static function public static function incrementBar() { self::$bar++; } } </code></pre> <p>Now see it in action:</p> <pre><code>// We haven't created any class instances yet (with the 'new' operator) // But we can access the static properties &amp; functions: echo A::$bar . " "; // prints 12345 A::incrementBar(); echo A::$bar . "\n"; // prints 12346 // Now we'll start working with class instances. // Create 2 instances of class A $a = new A(); $a-&gt;setFoo(8888); $b = new A(); $b-&gt;setFoo(9999); // It's a violation of strict standards to call getFoo() statically // And it's meaningless to do so, because $foo only exists inside a class instance! // Can't do this... Issues a strict warning since we're calling non-static getFoo() statically //echo A::getFoo(); // But we can call getFoo() on the class instances: echo $a-&gt;getFoo() . " " . $b-&gt;getFoo() . "\n"; // Prints 8888 9999 // Remember $bar was 12346... echo $a::$bar . " " . $b::$bar . "\n"; // Prints 12346 12346 // Now modify the static property $bar again // This affects all class instances. A::incrementBar(); echo $a::$bar . " " . $b::$bar . "\n"; // Prints 12347 12347 </code></pre> <p>I stuffed this whole thing into the codepad as well: <a href="http://codepad.viper-7.com/tV6omK" rel="noreferrer">http://codepad.viper-7.com/tV6omK</a></p> <p>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 <code>$this-&gt;property</code>, it will be a fatal error. You can't do that.</p> <p>In PHP's <code>error_reporting</code>, the setting of <code>E_ALL</code> for show all errors actually does not include strict warnings. That has to be done with <code>E_ALL &amp; E_STRICT</code>.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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