note 124820 added to language.references.return

From: Date: Thu, 12 Mar 2020 11:41:10 +0000
Subject: note 124820 added to language.references.return
Groups: php.notes 
Request: Send a blank email to php-notes+get-214320@lists.php.net to get a copy of this message
You can set the value of the variable returned by reference, be it a static function variable or a private property of an object (which is quite dangerous o.o). Static function variable: <?php function &func(){ static $static = 0; return $static; } $var1 =& func(); echo "var1:", $var1, "\n"; // 0 func(); $var1 = 90; echo "var1:", $var1, "\n"; // 90 echo "static:", func(), "\n"; // 90 ?> Private property <?php class foo { private $value = 1; public function &getValue() { return $this->value; } public function setValue($val) { $this->value = $val; } } $obj = new foo; $myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 1. echo $obj->getValue(); // 1 echo $myValue; // 1 $obj->setValue(5); echo $obj->getValue(); // 5 echo $myValue; // 5 $myValue = 1000; echo $obj->getValue(); // 1000 echo $myValue; // 1000 ?> ---- Server IP: 45.112.84.4 Probable Submitter: 38.142.137.34 (proxied: 108.162.163.36) ---- Manual Page -- http://php.net/manual/en/language.references.return.php Edit -- https://master.php.net/note/edit/124820 Del: integrated -- https://master.php.net/note/delete/124820/integrated Del: useless -- https://master.php.net/note/delete/124820/useless Del: bad code -- https://master.php.net/note/delete/124820/bad+code Del: spam -- https://master.php.net/note/delete/124820/spam Del: non-english -- https://master.php.net/note/delete/124820/non-english Del: in docs -- https://master.php.net/note/delete/124820/in+docs Del: other reasons-- https://master.php.net/note/delete/124820 Reject -- https://master.php.net/note/reject/124820 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214320) next »