The problem of placing PHP objects

I am trying to create a circular linked list in php, which is pretty simple since php objects are links. This means that the behavior of php objects should be similar to C ++ pointers. The following is a simplified implementation of a linked list:

class Node{ public $next; } $node1 = new Node(); $node2 = new Node(); $node1->next = $node2; $node2->next = $node1; 

After implementing this, I realized that crazy things begin to happen when you link your objects in a circular list. for example, you get an error when comparing these objects using the operator:

 if($node1 == $node2) //Fatal error: Nesting level too deep - recursive dependency? 

I found the right way to compare these objects is to use strict comparison.

 if($node1 === $node2) //Works fine 

I think that a tight comparison is trying to compare all the attributes of an object. In doing so, he discovers that there is an infinite investment, so he reports an error. But I can not understand why the following works:

 if($node1->next == $node2) //Works fine with == rather than === 

Output:

Always use the identification operator (===) to compare objects - if you do not want the comparison to return true for similar objects with different instances, but keep in mind the nesting problem.

+4
source share
2 answers

As far as I know, PHP compares two objects only by looking at their properties indefinitely to the end.

if ($ node1 == $ node2)

  $node1 == $node2? if $node1 -> next == $node2->next if $node1->next(node2)->next == $node2->next(node1)->next if $node1->next(node2)->next(node1)->next == $node2->next(node1)->next(node2)->next; 

Infinitely...

If an object has a property, it will look at them if you are comparing two objects.

So why does your rigorous comparison work, because it just checks to see if two objects are in the same place in memory , and therefore it doesn't even look at the values ​​of the properties.

if ($ node1-> next == $ node2)

Why compare if ($ node1-> next == $ node2) , I think, as shown below: PHP first compares the "address" if the two objects to be compared have the same address. PHP assumes that they must be identical because they are in the same place .

Like $ node1 has address 1. And $ node2 has address 2. $ node2-> next then have address 1. The same address, PHP does not want to view the properties now, because they are in the same place.

Also for your reference. The comparison result "==" and "===" respectively on php.net http://php.net/manual/en/language.oop5.object-comparison.php

 Two instances of the same class o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : FALSE o1 !== o2 : TRUE Two references to the same instance o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 : TRUE o1 !== o2 : FALSE Instances of two different classes o1 == o2 : FALSE o1 != o2 : TRUE o1 === o2 : FALSE o1 !== o2 : TRUE 
+2
source

As you conclude in mapping php objects using "==", comparing the attributes of an object is implied and can become recursive

Using the comparison operator (==), object variables are compared in a simple way, namely: two instances of an object are equal if they have the same attributes and values ​​and are instances of the same class.

On the other hand, when using the identification operator (===) object variables are the same if and only if they relate to the same instance of the same class.

Now in the latter case, this works because you are comparing node2 with node2. For example:

 if($node2 == $node2) //works since there it can conclude in 1 step both are same objects 

a

 if($node2->next == $node2) //will still have the recursion issue since it has to follow the links 
+1
source

Source: https://habr.com/ru/post/1501934/


All Articles