If you want to test the existance of objects in arrays like in_array but with very strict checking in PHP4 (in order to avoid the "Nesting level too deep - recursive dependency?" error). i wrote the following function.
All objects that can be compared must extends comparable and call the comparable constructor. It will then create a serial number different on each objects.
I don't think it is the ultimate solution because arrays can contains loops and this won't be checked here. And if there is a recursive dependency between objects that do not extends comparable, you will still have the error.
Sometimes I think I don't like PHP very much ...
<?php
class comparable {
// Don't forget to call this constructor in sub-classes
function comparable(){
if(!isset($GLOBALS[__FILE__.'/class:comparable']))
$GLOBALS[__FILE__.'/class:comparable'] = 1;
$this->comparableSerial = $GLOBALS[__FILE__.'/class:comparable']++;
}
}
function exact_in_array(&$needle, &$haystack){
foreach($haystack as $k=>$v){
$elem = &$haystack[$k];
if(
is_object($needle) and is_object($elem) and
is_a($needle, 'comparable') and is_a($elem, 'comparable'))
{
if( get_class($needle) === get_class($elem) and
$needle->comparableSerial == $elem->comparableSerial)
return true;
}else{
// Uncomment this line to know which classes do not extends
// comparable and fail the test with Nesting level too deep error
//printf(
// "%s(%s) === %s(%s)",
// $needle, get_class($needle), $elem, get_class($elem));
if($needle === $elem) return true;
}
unset($elem);
}
return false;
}
?>
Comparando Objetos
No PHP 4, objetos são comparados de uma maneira bem simples: Duas instâncias de objetos são iguais se eles tem os mesmos atributos e valores, e se de instâncias da mesma classe. Regras similares são aplicadas quando comparando dois objetos utilizando o operador idêntico (===).
Executando o exemplo abaixo:
Exemplo #1 Exemplo de comparação de objetos no PHP 4
<?php
function bool2str($bool) {
if ($bool === false) {
return 'FALSE';
} else {
return 'TRUE';
}
}
function compareObjects(&$o1, &$o2) {
echo 'o1 == o2 : '.bool2str($o1 == $o2)."\n";
echo 'o1 != o2 : '.bool2str($o1 != $o2)."\n";
echo 'o1 === o2 : '.bool2str($o1 === $o2)."\n";
echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."\n";
}
class Flag {
var $flag;
function Flag($flag=true) {
$this->flag = $flag;
}
}
class SwitchableFlag extends Flag {
function turnOn() {
$this->flag = true;
}
function turnOff() {
$this->flag = false;
}
}
$o = new Flag();
$p = new Flag(false);
$q = new Flag();
$r = new SwitchableFlag();
echo "Comparando instâncias criadas com os mesmo parâmetros\n";
compareObjects($o, $q);
echo "\nComparando instâncias criadas com parâmetros diferentes\n";
compareObjects($o, $p);
echo "\nComparando instâncias de uma classe base com uma classe estendida\n";
compareObjects($o, $r);
?>
O exemplo acima irá imprimir:
Comparando instâncias criadas com os mesmo parâmetros o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : TRUE o1 !== o2 : FALSE Comparando instâncias criadas com parâmetros diferentes o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE Comparando instâncias de uma classe base com uma classe estendida o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE
Que é o resultado que esperava-mos obter pelas regras de comparação acima. Somente instâncias com os mesmos valores de seus atributos e da mesma classe são considerados iguais e/ou idênticos.
Mesmo no caso onde temos composição de objetos, as mesmas regras de comparação se aplicam. No exemplo abaixo, nós criamos uma classe container que preserva um array associativo de objetos Flag.
Exemplo #2 Comparação de objetos compostos no PHP 4
<?php
class FlagSet {
var $set;
function FlagSet($flagArr = array()) {
$this->set = $flagArr;
}
function addFlag($name, $flag) {
$this->set[$name] = $flag;
}
function removeFlag($name) {
if (array_key_exists($name, $this->set)) {
unset($this->set[$name]);
}
}
}
$u = new FlagSet();
$u->addFlag('flag1', $o);
$u->addFlag('flag2', $p);
$v = new FlagSet(array('flag1'=>$q, 'flag2'=>$p));
$w = new FlagSet(array('flag1'=>$q));
echo "\nComparando objetos compostos u(o,p) e v(q,p)\n";
compareObjects($u, $v);
echo "\nu(o,p) e w(q)\n";
compareObjects($u, $w);
?>
O exemplo acima irá imprimir:
Comparando objetos compostos u(o,p) e v(q,p) o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : TRUE o1 !== o2 : FALSE u(o,p) e w(q) o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE
Comparando Objetos
16-May-2007 11:17
29-Mar-2006 12:15
If youre just checking to see if the variables both reference the same object, instead of having a variable set up beforehand you could do away with the GLOBALS and have something like..
e.g.
<?php
class Foo {
var $serial;
function Foo() {
// Rest of constructor...
}
// Rest of class definition...
}
$baz->serial = -1;
$bar->serial = time();
if($bar->serial == $baz->serial) {
echo "Same";
}
?>
29-Apr-2005 09:24
Using globals isn't really well... you can use pattern singleton to verify this.
09-Apr-2004 01:44
An addendum to the post below:
If you are comparing two objects and you know that they will be of the same type (you're just not sure if they refer to the same object or not), then there is an easier and faster way to do the comparison, and this also avoids the infinite recursion problem with circular references described in the post below.
In the constructor of your object, set a "serial" attribute from a global variable that is incremented whenever a new object is created. Then you just have to compare serial numbers to see if the objects are the same.
e.g.
<?php
$FooSerial = 0;
class Foo {
var $serial;
function Foo() {
$this->serial = $GLOBALS['FooSerial']++;
// Rest of constructor...
}
// Rest of class definition...
}
if($bar->serial == $baz->serial) {
echo "Same";
}
?>
08-Apr-2004 01:18
PHP4 compares two objects (say, $a and $b) by comparing the type and all the attributes. If an attribute is an object, PHP4 will recursively call "if($a.attribute === $b.attribute)" to determine equality. However, this is a problem if a circular reference has been formed. The recursion will go on forever, and you will get the error message:
"Fatal error: Nesting level too deep - recursive dependency?"
Example, where the comparison will never terminate because PHP4 will forever recurse with comparisons of the attribute.
<?php
class Test {
var $obj;
}
$foo =& new Test;
$bar =& new Test;
$foo->obj =& $bar; // Make circular reference
$bar->obj =& $foo;
if($foo === $bar) {
baz();
}
?>
First PHP4 does ($foo === $bar), which expands into ($foo.obj === $bar.obj), which expands into ($bar.obj === $foo.obj), and so on and so on.
To avoid this situation, you must compare objects manually by comparing the two object's attributes and avoiding comparisons on attributes where a circular reference could arise.
This issue is easily avoided in PHP5, where objects can be compared via references rather than the object contents.

Referências dentro do construtor