Dear internals,
I stumbled upon the following odd error message from PHP which I was not
expecting.
Here is a small example that triggered the error with 5.2.2, 5.2.3 and a
php5.2-200706052030 snapshot.
<?php
class class1{
public $c2;
function __construct($c2){
$this->c2 = $c2;
}
}
class class2{
public $c1;
function setC1($c1){
$this->c1 = $c1;
}
function test(){
echo $this == $this->c1->c2?'equals':'not equals'; // Somehow this
if statement triggers the error.
}
}
$tt = new class2();
$t = new class1($tt);
$tt->setC1($t);
var_dump($t);
$tt->test();
?>
I get the error:
Fatal error: Nesting level too deep - recursive dependency? in
/home/cyanox/DEV/test_object_recursion.php on line 15
Although it is obvious that there is a recursion I think this simple
example should work without error.
Greetings,
Mark Sanders.
<?php
class class1{
public $c2;
function __construct($c2){
$this->c2 = $c2;
}
}class class2{
public $c1;
function setC1($c1){
$this->c1 = $c1;
}
function test(){
echo $this == $this->c1->c2?'equals':'not equals'; // Somehow this
if statement triggers the error.
}
}$tt = new class2();
$t = new class1($tt);
$tt->setC1($t);
var_dump($t);
$tt->test();
?>I get the error:
Fatal error: Nesting level too deep - recursive dependency? in
/home/cyanox/DEV/test_object_recursion.php on line 15
$t->$tt->$t->$tt->$t->$tt->$t->$tt->$t->$tt->$t and so on.
So you get endless recursion when comparing the objects.
Although it is obvious that there is a recursion I think this simple
example should work without error.
Sure, if you have any proposals/ideas - feel free to share them.
--
Wbr,
Antony Dovgal
Antony Dovgal wrote:
<?php
class class1{
public $c2;
function __construct($c2){
$this->c2 = $c2;
}
}class class2{
public $c1;
function setC1($c1){
$this->c1 = $c1;
}
function test(){
echo $this == $this->c1->c2?'equals':'not equals'; // Somehow
this if statement triggers the error.
}
}$tt = new class2();
$t = new class1($tt);
$tt->setC1($t);
var_dump($t);
$tt->test();
?>I get the error:
Fatal error: Nesting level too deep - recursive dependency? in
/home/cyanox/DEV/test_object_recursion.php on line 15$t->$tt->$t->$tt->$t->$tt->$t->$tt->$t->$tt->$t and so on.
So you get endless recursion when comparing the objects.Although it is obvious that there is a recursion I think this simple
example should work without error.Sure, if you have any proposals/ideas - feel free to share them.
Actually theoretically it should not give an error. I just stumbled upon
this odd behavior when doing something very odd which would not work
anyway so I can't really think of a situation where the correct behavior
would be needed. Anyway the following also gives the error.
<?php
class class1{
public $c2;
function __construct($c2){
$this->c2 = $c2;
}
}
class class2{
public $c1;
function test(){
$this == $this->c1; // no error.
$this == $this; // errors.
}
}
$tt = new class2();
$t = new class1($tt);
$tt->c1 = $t;
$tt == $t; // no error.
//$t == $t; // errors.
//$tt == $tt; // errors.
$tt->test(); // errors.
?
Fatal error: Nesting level too deep - recursive dependency? in
/home/cyanox/DEV/test_object_recursion.php on line 15$t->$tt->$t->$tt->$t->$tt->$t->$tt->$t->$tt->$t and so on.
So you get endless recursion when comparing the objects.Although it is obvious that there is a recursion I think this simple
example should work without error.Sure, if you have any proposals/ideas - feel free to share them.
Actually theoretically it should not give an error.
Why?
The behavior is quite clear - if the objects are instances of the same class,
we have no other way to compare them but to compare their properties, this also
applies to properties' properties and properties' properties' properties etc.
I just stumbled upon
this odd behavior when doing something very odd which would not work
anyway so I can't really think of a situation where the correct behavior
would be needed. Anyway the following also gives the error.<?php
class class1{
public $c2;
function __construct($c2){
$this->c2 = $c2;
}
}class class2{
public $c1;
function test(){
$this == $this->c1; // no error.
Right.
$this and $this->c1 are class2 and class1, no need even to look at the properties
to say that they are not equal.
$this == $this; // errors.
This is what I described earlier.
If you have any ideas on how to improve it - I'd be happy to hear them.
--
Wbr,
Antony Dovgal
Dear internals,
I stumbled upon the following odd error message from PHP which I was
not
expecting.Here is a small example that triggered the error with 5.2.2, 5.2.3 and
a
php5.2-200706052030 snapshot.<?php
class class1{
public $c2;
function __construct($c2){
$this->c2 = $c2;
}
}class class2{
public $c1;
function setC1($c1){
$this->c1 = $c1;
}
function test(){
echo $this == $this->c1->c2?'equals':'not equals'; // Somehow this
if statement triggers the error.
}
}$tt = new class2();
$t = new class1($tt);
$tt->setC1($t);
var_dump($t);
$tt->test();
?>I get the error:
Fatal error: Nesting level too deep - recursive dependency? in
/home/cyanox/DEV/test_object_recursion.php on line 15Although it is obvious that there is a recursion I think this simple
example should work without error.
I also don't see why it would recurse infinitely...
Seems like it should be pretty straight-forward, unless I'm missing
something...
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
mark@cyanox.nl wrote:
Dear internals,
I stumbled upon the following odd error message from PHP which I was not
expecting.
function test(){
echo $this == $this->c1->c2?'equals':'not equals'; // Somehow this
Use === (shallow test - returns true iff the variables refer to the same
instance) instead of == (deep comparision - member-by-member).
AFAIK this behaviour was introduced in 5.2 or 5.1 - well, just another BC
break.
--
Paweł Stradomski