note 98352 added to language.oop5.decon
| From: | Anonymous at osu1 dot php dot net | Date: | Thu, 10 Jun 2010 11:50:08 +0000 |
| Subject: | note 98352 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170015@lists.php.net to get a copy of this message | ||
Be careful when using a class as reference or copy in another one in regard of the __destruct()
method:
<?php
class MyMainClass {
private $tables = array();
public function __construct() {
echo "Create ",get_class($this), " Object!<br>";
}
public function __destruct() {
echo "Destroy ",get_class($this)," Object!<br>";
}
public function &getSubClass($name) {
if(!isset($this->tables[$name])) {
# This will result in NOT calling the __destruct() method when
# unsetting an objct of MyMainClass
$this->tables[$name] = new MySubClass($this, $name);
# If you clone the class (for testing) it will work as expected
// $this->tables[$name] = new MySubClass(clone $this, $name);
}
return $this->tables[$name];
}
}
class MySubClass {
private $name = null;
private $main_class = null;
public function __construct(MyMainClass &$main_class, $name) {
echo "Create ",get_class($this), " '$name' Object!<br>";
$this->name = $name;
$this->main_class = $main_class;
}
public function __destruct() {
echo "Destroy ",get_class($this)," '$this->name'
Object!<br>";
}
}
$my = new MyMainClass($logger);
$my->getSubClass('bar');
$my->getSubClass('foo');
unset($my);
echo "_____End of skript reached!_____<br>";
?>
This will result in:
Create MyMainClass Object!
Create MySubClass 'bar' Object!
Create MySubClass 'foo' Object!
_____End of Skript reached!_____
Destroy MyMainClass Object!
Destroy MySubClass 'bar' Object!
Destroy MySubClass 'foo' Object!
----
Server IP: 69.147.83.197
Probable Submitter: 91.20.221.43
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/98352
Del: integrated -- https://master.php.net/note/delete/98352/integrated
Del: useless -- https://master.php.net/note/delete/98352/useless
Del: bad code -- https://master.php.net/note/delete/98352/bad+code
Del: spam -- https://master.php.net/note/delete/98352/spam
Del: non-english -- https://master.php.net/note/delete/98352/non-english
Del: in docs -- https://master.php.net/note/delete/98352/in+docs
Del: other reasons-- https://master.php.net/note/delete/98352
Reject -- https://master.php.net/note/reject/98352
Search -- https://master.php.net/manage/user-notes.php