<?php
function bool2str($bool)
{
if ($bool === false) {
return 'FALSE';
} else {
return 'TRUE';
}
}
function compareObjects(&$o1, &$o2)
{
echo 'o1 == o2 : ' . bool2str($o1->equals($o2)) . "\n";
echo 'o1 != o2 : ' . bool2str($o1 instanceof $o2) . "\n";
}
class Flag
{
public $flag;
function Flag($flag = true) {
$this->flag = $flag;
}
}
class OtherFlag
{
public $flag;
function OtherFlag($flag = true) {
$this->flag = $flag;
}
}
$o = new Flag();
$p = new Flag();
$q = $o;
$r = new OtherFlag();
echo "Два экземпляра одного и того же класса\n";
compareObjects($o, $p);
echo "\nДве ссылки на один и тот же экземпляр\n";
compareObjects($o, $q);
echo "\nЭкземпляры двух разных классов\n";
compareObjects($o, $r);
Два экземпляра одного и того же класса
Fatal error: Uncaught Error: Call to undefined method Flag::equals() in /in/P4hSI:13
Stack trace:
#0 /in/P4hSI(41): compareObjects(Object(Flag), Object(Flag))
#1 {main}
thrown in /in/P4hSI on line 13
Process exited with code 255.
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Flag has a deprecated constructor in /in/P4hSI on line 17
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; OtherFlag has a deprecated constructor in /in/P4hSI on line 26
Два экземпляра одного и того же класса
Fatal error: Uncaught Error: Call to undefined method Flag::equals() in /in/P4hSI:13
Stack trace:
#0 /in/P4hSI(41): compareObjects(Object(Flag), Object(Flag))
#1 {main}
thrown in /in/P4hSI on line 13
Process exited with code 255.