note 104296 added to language.oop5.overloading
| From: | jk at jankriedner dot de | Date: | Tue, 07 Jun 2011 10:15:54 +0000 |
| Subject: | note 104296 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180306@lists.php.net to get a copy of this message | ||
You should take care when using properties retrieved via __get() in functions that expect arguments
to be passed by reference (e.g. mysqli_stmt_bind_param). The reference is NOT set to the property
itself, but to the value returned by __get().
Thus, binding a property retrieved via __get() to a statement will let the statement be executed
always with the value the property had when calling bind_param, not with the current value it has
when calling execute().
E.g.:
<?php
error_reporting(E_ALL);
class foo {
protected $bar;
public function __construct() {
$this->bar = "Hello World!";
}
public static function factory() {
return new self;
}
public function __get($property) {
if(!property_exists($this,$property)) {
throw new InvalidArgumentException("Property {$property} doesn't exist");
}
return $this->$property;
}
public function setBar($value) {
$this->bar = $value;
}
}
$foo = new foo();
echo $foo->bar; // Ouputs: Hello World!
$db = new mysqli("localhost","root","","tests");
$sql = "INSERT INTO foo SET bar=?";
$res = $db->prepare($sql);
$res->bind_param("s",$foo->bar); // Notice: Indirect modification of overloaded
property foo::$bar has no effect in /var/www/overload.php on line 24
$res->execute(); // Writes "Hello World!" to database
$foo->setBar("Goodbye");
echo $foo->bar; // Outputs: Goodbye
$res->execute(); // Writes "Hello World!" to database
?>
----
Server IP: 69.147.83.197
Probable Submitter: 79.194.68.100
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/104296
Del: integrated -- https://master.php.net/note/delete/104296/integrated
Del: useless -- https://master.php.net/note/delete/104296/useless
Del: bad code -- https://master.php.net/note/delete/104296/bad+code
Del: spam -- https://master.php.net/note/delete/104296/spam
Del: non-english -- https://master.php.net/note/delete/104296/non-english
Del: in docs -- https://master.php.net/note/delete/104296/in+docs
Del: other reasons-- https://master.php.net/note/delete/104296
Reject -- https://master.php.net/note/reject/104296
Search -- https://master.php.net/manage/user-notes.php