Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>In Very Simple English:</h2> <p>Once inside an object's function, you have complete access to its variables, but to set them you need to be more specific than just using the variable name you want to work with. To properly specify you want to work with a local variable, you need to use the special <code>$this</code> variable, which PHP always sets to point to the object you are currently working with.</p> <p>For example: </p> <pre><code>function bark() { print "{$this-&gt;Name} says Woof!\n"; } </code></pre> <p>Whenever you are inside a function of an object, PHP automatically sets the <code>$this</code> variable contains that object. You do not need to do anything to have access to it.</p> <hr> <h2>In Normal English:</h2> <p><code>$this</code> is a pseudo-variable which is available when a method is called from within an object context. It is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object)</p> <p><strong>An example:</strong></p> <pre><code>&lt;?php class A { function foo() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")\n"; } else { echo "\$this is not defined.\n"; } } } class B { function bar() { // Note: the next line will issue a warning if E_STRICT is enabled. A::foo(); } } $a = new A(); $a-&gt;foo(); // Note: the next line will issue a warning if E_STRICT is enabled. A::foo(); $b = new B(); $b-&gt;bar(); // Note: the next line will issue a warning if E_STRICT is enabled. B::bar(); ?&gt; </code></pre> <p><strong>Output:</strong></p> <pre><code>$this is defined (A) $this is not defined. $this is defined (B) $this is not defined. </code></pre>
 

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