note 90667 added to language.oop5.basic
| From: | webmaster at oehoeboeroe dot nl | Date: | Mon, 04 May 2009 00:03:11 +0000 |
| Subject: | note 90667 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154168@lists.php.net to get a copy of this message | ||
If you pass $this by reference and then assign a new value to it, it will not behave as you might
expect as illustrated by this example:
<?php
class TestClass
{
public $data;
function setData(&$data) {
$this->data =& $data;
}
function hasData(&$data) {
$saved = $data;
$data = true;
$result = $this->data === true;
$data = $saved;
return $result;
}
function isDataOf(&$object) {
return $object->hasData($this);
}
}
$o1 = new TestClass;
$o2 = new TestClass;
$o1->setData($o2);
$o2->setData($o1);
var_dump($o1->hasData($o2)); // true as expected
var_dump($o2->hasData($o1)); // true as expected
var_dump($o1->isDataOf($o2)); // false even though $o1 is in fact the data of $o2
var_dump($o2->isDataOf($o1)); // false even though $o2 is in fact the data of $o1
?>
You can make this example work by replacing the hasData method with:
<?php
function hasData(&$data) {
return $data === $this->data;
}
?>
However, although I've not tested this, I've been told that Zend Engine 1, i.e. PHP 4,
will choke on the === parameter when comparing recursing objects.
----
Server IP: 69.147.83.197
Probable Submitter: 82.92.197.212
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- https://master.php.net/note/edit/90667
Del: integrated -- https://master.php.net/note/delete/90667/integrated
Del: useless -- https://master.php.net/note/delete/90667/useless
Del: bad code -- https://master.php.net/note/delete/90667/bad+code
Del: spam -- https://master.php.net/note/delete/90667/spam
Del: non-english -- https://master.php.net/note/delete/90667/non-english
Del: in docs -- https://master.php.net/note/delete/90667/in+docs
Del: other reasons-- https://master.php.net/note/delete/90667
Reject -- https://master.php.net/note/reject/90667
Search -- https://master.php.net/manage/user-notes.php