If one were to extend a built in class that implements Traversable would it be possible to obtain a reference to the root object perhaps with debug_backtrace?
For example if I have
$foo->bar->baz->biz->bon->bop->bob();
and in the bob method of bop I make use of debug_backtrace is there a way to get a reference to $foo? (And what is that way?)
If so is this even the most elegant or efficient way to do that?
I've tried looking at the debug_backtrace php.net page and I am still not the clear how to really use this function but from other material and experimentation I have my doubts (see update).
Update #1:
There does seem to be some debate on if debug_backtrace should be left in production code. PHP debug_backtrace in production code to get information about calling method?
Of course part of this is the issue of coupling, should a called object have knowledge of the object calling it?
Update #2
After finding Find out which class called a method in another class I tried my hand at using what I have learned to reverse traverse using debug_backtrace and discovered that it is not likely to be possible.
<?php
class mother extends SimpleXMLElement {
protected $test_a = 'foo';
public $test_b = 'bar';
public function shout(){
echo "test_a '" , $this->test_a , "' while test_b '" , $this->test_b , "'.
";
}
public function tell_mother($message,$limit=42){
$trace = debug_backtrace();
--$limit;
if($limit<1){
echo "Enough of that pointlessness
";
var_dump($trace);
return 0;
}
if ( isset($trace[1]) && isset($trace[1]['object']) ){
// $trace[1] is the caller class
echo "I will pass the message on
";
$trace[1]['object']->tell_mother($message,$limit);
}else{
echo "I am a " , get_class($this) , "
";
echo "I have been told \"{$message}\".
";
var_dump($trace);
}
}
}
echo "<pre>";
$xml = <<<XML
<?xml version='1.0'?>
<a>lob
<b>tob
<c>bob
<d>boo
<e>bin
<f>baz
<g>bar
<h>foo</h>
</g>
</f>
</e>
</d>
</c>
</b>
</a>
XML;
$obj = simplexml_load_string($xml,'mother');
$obj->b->c->d->e->f->g->h->tell_mother("I love her");
$obj->shout();
$obj->b->c->d->e->f->shout();
As far as I can tell debug_backtrace cannot reverse traverse and no values held in object scope can be accessed.
Output from the above gives
I am a mother
I have been told "I love her".
array(1) {
[0]=>
array(7) {
["file"]=>
string(58) "/home/[[snip]]_test.php"
["line"]=>
int(64)
["function"]=>
string(11) "tell_mother"
["class"]=>
string(6) "mother"
["object"]=>
object(mother)#2 (1) {
[0]=>
string(3) "foo"
}
["type"]=>
string(2) "->"
["args"]=>
array(1) {
[0]=>
&string(10) "I love her"
}
}
}
test_a '' while test_b ''.
test_a '' while test_b ''.
My conclusion is that internally PHP does not see the child elements as being called by the parents. So I have edited my question to simply ask if it is possible to reverse traverse a Traversable class.