Page 1 of 1

linked list, arrays and references to objects ???

Posted: Tue Jul 23, 2002 8:23 am
by neh
Hi

back again - I'm scratching my head again here ..

I am using a design pattern known as intercepting filter which is based on a simple linked list .

when I instantiate my filterroot I send an array of other concrete filters and the constructor iterates through them and sets the _childFilter and _hasChild attributes acordingly in a simple while loop

Code: Select all

class FilterChainRoot
  {

   var $_filters;

   function FilterChainRoot($filters)
   {
    //iterate through the InterceptingFilter array and
    //add them to the chain of filters to execute
    $this->_filters =& $filters;
    $count = count($this->_filters);
    $index = 0;

    while ($index <= ($count - 1)):
           if ($index > 0)
           &#123;
           	   $tmp =& $this->_filters&#1111;$index - 1];
               $tmp->setChild($filters&#1111;$index]);
               //debug <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>		
               //echo get_class($tmp)." child is set as - ".get_class($tmp->getChild())."<br>";
           &#125;
           $index++;
    endwhile;
   &#125;
    
   function execute($args)
   &#123;
    //perform first execute in our chain
    //each filter in the chain will check whether
    //it has a child .. if so it will apss on its
    //execute down the chain
    $tmp = $this->_filters&#1111;0];
    return $tmp->execute($args);
   &#125;
  &#125;
I then call the first execute to start the chain which should walk the app through the filterchain until finally returning to the root caller with my html response ..

this is done by each execute method inside a concrete InterceptingFilter class first checks whether it has a child filter then calls its execute method

eg

Code: Select all

...
if ($this->_hasChild)
    &#123;
    	$tmp =& $this->_childFilter;
        return  $tmp->execute($arg);
    &#125;

...
now this is all working fine until I reach the last but branch node which should point to my frontcontroller in its childfilter attribute .. but instead its child is left dangling at null and I get an error as I try to call a method on a null reference basically .. I gone through it loads of times trying to figure how I've buggered it up but am at a loss ..

the offending snippet is below

Code: Select all

...

$obj =& $this->_childFilter;
$response = $obj->execute($args);
when debugging I have checked that its child is set properly with a reference to my frontcontroller and it seems to set just fine when I check in the root constructor but when this line executes it always falls over with ..
Fatal error: Call to a member function on a non-object in C:\Documents and Settings\ant\My Documents\objectmonkey_source\ViewFilter.php on line 13

if any of you have any ideas where I am going wrong it would be very greatly appreciated .. thanks :)

Posted: Tue Jul 23, 2002 8:47 am
by BDKR
In looking at the below code, I am curious about what you are trying to do.

Code: Select all

$obj =& $this->_childFilter;
Are you trying to create an object name $obj? That's what it looks like. If that's the case, whats the name of the object that contains "_childFilter"? I'm not even sure that you can do this, but try something like...

Code: Select all

$obj = &$name_of_the_object->this->childFilter;
If that works, then the below line should work as well.

Code: Select all

$response = $obj->execute($args);
Later on,
BDKR

Posted: Tue Jul 23, 2002 8:50 am
by neh
figured it meself again ... :) need to pass a reference into the setchild method to make sure I'm not doing a deep copy :) .. w00t ..

Posted: Tue Jul 23, 2002 8:54 am
by neh
BDKR wrote:In looking at the below code, I am curious about what you are trying to do.

Code: Select all

$obj =& $this->_childFilter;
Are you trying to create an object name $obj? That's what it looks like. If that's the case, whats the name of the object that contains "_childFilter"? I'm not even sure that you can do this, but try something like...

Code: Select all

$obj = &$name_of_the_object->this->childFilter;
If that works, then the below line should work as well.

Code: Select all

$response = $obj->execute($args);
Later on,
BDKR
didnt follow that at all I'm afraid ... I figured it meself anyway .. but to answer your question I am getting a reference to a reference of an object I stored as $this's _childFilter attribute .. but using multiple arrow operators doesnt work anyway (ie $var->method_or_att->call(); will not work)