Object identity check
Moderator: General Moderators
Object identity check
How do you test whether two variable are referencing the same object? I have two instances of $db, and I'm not sure whether they reference the same object... how can I check?
In php5 you can use ===
Code: Select all
<?php
class foobar {
private $xyz;
public function __construct($xyz) {
$this->xyz = $xyz;
}
}
$f1 = new foobar(1);
$f2 = $f1;
$f3 = clone $f1;
$f4 = new foobar(4);
echo '$f1===$f2: ', $f1===$f2 ? 'true':'false', "<br />\n";
echo '$f1===$f3: ', $f1===$f3 ? 'true':'false', "<br />\n";
echo '$f1===$f4: ', $f1===$f4 ? 'true':'false', "<br />\n";
?>