note 116927 added to language.variables.basics

From: Date: Sun, 22 Mar 2015 12:05:43 +0000
Subject: note 116927 added to language.variables.basics
Groups: php.notes 
Request: Send a blank email to php-notes+get-202009@lists.php.net to get a copy of this message
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. ---- Server IP: 89.187.142.233 Probable Submitter: 62.197.243.253 ---- Manual Page -- http://php.net/manual/en/language.variables.basics.php Edit -- https://master.php.net/note/edit/116927 Del: integrated -- https://master.php.net/note/delete/116927/integrated Del: useless -- https://master.php.net/note/delete/116927/useless Del: bad code -- https://master.php.net/note/delete/116927/bad+code Del: spam -- https://master.php.net/note/delete/116927/spam Del: non-english -- https://master.php.net/note/delete/116927/non-english Del: in docs -- https://master.php.net/note/delete/116927/in+docs Del: other reasons-- https://master.php.net/note/delete/116927 Reject -- https://master.php.net/note/reject/116927 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202009) next »