note 116927 deleted from language.variables.basics by cmb

From: Date: Sun, 28 Apr 2019 17:54:12 +0000
Subject: note 116927 deleted from language.variables.basics by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-210865@lists.php.net to get a copy of this message
Note Submitter: Kubo2 ---- What's funny with PHP's $this variable and variable variables: Imagine you have a class like below: <?php class Bar { private $foo = 9; function __construct() { $varName = 'this'; $$varName = 'text'; // sets $this to 'text' // outputs 'text' echo $this; // instead of Notice: Trying to get property of non-object, outputs 9 echo $this->foo; } } $a = new Bar; var_dump($a); // object(Bar)#1 (1) {...} ?> So PHP doesn't rely on $this's value itself, but rather on its name and the syntax you're accessing instance's properties and calling its methods. Also it doesn't pass any hidden parameter $this to the instance's methods nor it automatically returns $this from the __construct()or, like often being described in books which tries to teach OOP. $this is simply an undefined variable with special meaning of its name.

« previous php.notes (#210865) next »