tags:

views:

61

answers:

2

Hi Guys,

How come you can't use the Class's traversing methods with variable's to set the parameters

For instance Using this array:

array(0 => array('element' => 'img[src=images/more.gif]', 'attribute' => 'parent()->href'));

this works:

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
{
 $this->store[$keyz] = $found->parent()->href
}

But this doesn't:

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
    {
     $this->store[$keyz] = $found->$target[$key]['attribute'];
    }

I have tried changing they array like so:

array(0 => array('element' => 'img[src=images/more.gif]', 'traverse' => 'parent()', 'attribute' => 'href')

And then trying:

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
        {
         $this->store[$keyz] = $found->$target[$key]['traverse']->$target[$key]['attribute'];
        }

doesn't work.

On both failures a print_r($this->store) simply gives Array();

+1  A: 

It's got nothing to do with that slow Simple HTML thingamyig: this is not how PHP works, your string parent()->href won't be interpreted as a call to those elements. If you need this, you are on the right track, but you have to distinguish functions & atributes. Somethink like either:

array(
  'element' => 'img[src=images/more.gif]',
  'traverse' => array(
     array('parent','function'),
     array('attribute' ,'property');
...

$result = $found
foreach($target[$key]['traverse'] as $step){
   switch($step[1]){
       case 'function':
           $function = $step[0];
           $result = $found->$function();
           break;
       case 'property':
           $property = $step[0];
           $result = $found->$property;
           break;
       default:
           trigger_error("Unknown step method ".$step[1].": not an property or function",E_USER_ERROR);
   }
}
$this->store[$keyz] = $result;

Or this could work with your original strings:

array(
   'element' => 'img[src=images/more.gif]',
   'attribute' => 'parent()->href'));

...
$result = $found;
foreach(explode('->',$target[$key]['attribute']) as $step){
    if(substr($step,-2) == '()'){
        $function = substr($step,0, strlen($step)-2);
        $result = $result->$function();
    } else {
        $result = $result->$step;
    }
}
$this->store[$keyz] = $result;
Wrikken
A: 

Thank you Wrikken, based on your advice I came up with this, which works perfectly

foreach($this->contents->find($target[$key]['element']) as $keyz => $found) 
            {
             if (!isset($target[$key]['traverse']) && !isset($target[$key]['attribute']))
             {
             $this->store[] = $found;
             }
             else if (isset($target[$key]['traverse']) && !isset($target[$key]['attribute']) 
             {
            $function = $target[$key]['traverse'];
            $this->store[] = $found->$function();            
           }
             else if (isset($target[$key]['traverse']) && isset($target[$key]['attribute']))
             {
              $function = $target[$key]['traverse'];
            $this->store[] = $found->$function()->$target[$key]['attribute'];       
             }
            }
         }
Liam Bailey