note 93939 added to language.oop5.overloading
| From: | strata_ranger at hotmail dot com | Date: | Wed, 07 Oct 2009 16:56:46 +0000 |
| Subject: | note 93939 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161478@lists.php.net to get a copy of this message | ||
Combining two things noted previously:
1 - Unsetting an object member removes it from the object completely, subsequent uses of that member
will be handled by magic methods.
2 - PHP will not recursively call one magic method from within itself (at least for the same $name).
This means that if an object member has been unset(), it IS possible to re-declare that object
member (as public) by creating it within your object's __set() method, like this:
<?php
class Foo
{
function __set($name, $value)
{
// Add a new (public) member to this object.
// This works because __set() will not recursively call itself.
$this->$name= $value;
}
}
$foo = new Foo();
// $foo has zero members at this point
var_dump($foo);
// __set() will be called here
$foo->bar = 'something'; // Calls __set()
// $foo now contains one member
var_dump($foo);
// Won't call __set() because 'bar' is now declared
$foo->bar = 'other thing';
?>
Also be mindful that if you want to break a reference involving an object member without triggering
magic functionality, DO NOT unset() the object member directly. Instead use =& to bind the
object member to any convenient null variable.
----
Server IP: 64.71.164.2
Probable Submitter: 67.237.228.46
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/93939
Del: integrated -- https://master.php.net/note/delete/93939/integrated
Del: useless -- https://master.php.net/note/delete/93939/useless
Del: bad code -- https://master.php.net/note/delete/93939/bad+code
Del: spam -- https://master.php.net/note/delete/93939/spam
Del: non-english -- https://master.php.net/note/delete/93939/non-english
Del: in docs -- https://master.php.net/note/delete/93939/in+docs
Del: other reasons-- https://master.php.net/note/delete/93939
Reject -- https://master.php.net/note/reject/93939
Search -- https://master.php.net/manage/user-notes.php