/ Published in: PHP
Here are a few examples that show how the relationship between objects and references
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class Box{ public $name = "box"; } $box = new Box(); $box_reference = $box; $box_clone = clone $box; $box_changed = clone $box; $box_changed->name = "changed box"; $another_box = new Box(); // Attributes are pretty much the same echo $box == $box_reference ? 'true' : 'false'; echo "<br />"; echo $box == $box_clone ? 'true' : 'false'; echo "<br />"; echo $box == $box_changed ? 'true' : 'false'; echo "<br />"; echo $box == $another_box ? 'true' : 'false'; echo "<br />"; echo "<br />"; // Checks to see if they reference the same object echo $box === $box_reference ? 'true' : 'false'; echo "<br />"; echo $box === $box_clone ? 'true' : 'false'; echo "<br />"; echo $box === $box_changed ? 'true' : 'false'; echo "<br />"; echo $box === $another_box ? 'true' : 'false'; echo "<br />"; ?>