note 97348 deleted from language.oop5.cloning by cmb
| From: | cmb@php.net | Date: | Mon, 03 Dec 2018 17:25:16 +0000 |
| Subject: | note 97348 deleted from language.oop5.cloning by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-209846@lists.php.net to get a copy of this message | ||
Note Submitter: jeffrey at whinger dot nl
----
For me it wasn't very clear to how this cloning of objects really worked so I made this little
bit of code:
<?php
class foo
{
public $test;
public function test()
{
echo 'give us a '.$this->test."<br>\n";
}
}
class bar
{
public $foo;
public function insertFoo($foo)
{
$this->foo = $foo;
}
}
$foo = new foo();
$foo->test = 'foo';
$bar = new bar();
$bar->insertFoo($foo);
$foo->test();
$bar->foo->test();
$foo->test = 'bar';
$foo->test();
$bar->foo->test();
$bar->foo = clone $foo;
$bar->foo->test = 'woop woop';
$foo->test();
$bar->foo->test();
// result:
// give us a foo
// give us a foo
// give us a bar
// give us a bar
// give us a bar
// give us a woop woop
?>