Page 1 of 1

Stupid errors in PHP5 OOP -- Parent object seems to be acces

Posted: Thu Nov 17, 2005 4:01 am
by Chris Corbyn
Fatal error: Cannot access private property exclusionPDF::$pdfparts in /path/to/class.pdf.php on line 3053
OK. So I'm writing an extension to a class called ezPdf (which is in itself an extension to a class called "pdf").

For some reason I'm getting the above error. I havent touched the original class, I'm simply inheriting from it. Yes I do have a private property called "pdfparts" in my extnesion but why on earth would the parent class be trying to access it? :?

I can't suss this out. Here's the extension (queries obscured...):

CODE REMOVED FOR LEGAL REASONS

I've been looking for a while and I've probably missed something obvious with the $pdfparts property but I can't see it :? If the issue "is" in my extension then that error is pretty lame as far as useful errors go. If it's in the parent class.... then it shouldn't be :P

Lines 3050 to 3060 of the parent of the parent (two generations up).

Code: Select all

$tmp = $this->checkpoint;
        foreach ($tmp as $k=>$v){
          if ($k != 'checkpoint'){
            $this->$k=$v;   // LINE 3053!!!!
          }
        }
        unset($tmp);
      }
      break;
    case 'abort':
      if (is_array($this->checkpoint)){
WTF 8O

Posted: Thu Nov 17, 2005 4:15 am
by Chris Corbyn
Hmmm.... it turns out the parent is indeed trying to access it's own child properties.

That $this->$k has $k == 'pdfparts' or whatever the heck I call the property in my extension for that matter.

Seems weird but I guess this is one I'll have to suss out on my own :(

Posted: Thu Nov 17, 2005 4:55 am
by Chris Corbyn

Code: Select all

/**
* a few functions which should allow the document to be treated transactionally.
*/
function transaction($action){
  switch ($action){
    case 'start':
      // store all the data away into the checkpoint variable
      $data = get_object_vars($this);
      $this->checkpoint = $data;
      unset($data);
      break;
    case 'commit':
      if (is_array($this->checkpoint) && isset($this->checkpoint['checkpoint'])){
        $tmp = $this->checkpoint['checkpoint'];
        $this->checkpoint = $tmp;
        unset($tmp);
      } else {
        $this->checkpoint='';
      }
      break;
    case 'rewind':
      // do not destroy the current checkpoint, but move us back to the state then, so that we can try again
      if (is_array($this->checkpoint)){
        // can only abort if were inside a checkpoint
        $tmp = $this->checkpoint;
        foreach ($tmp as $k=>$v){
          if ($k != 'checkpoint'){
            $this->$k=$v;
          }
        }
        unset($tmp);
      }
      break;
    case 'abort':
      if (is_array($this->checkpoint)){
        // can only abort if were inside a checkpoint
        $tmp = $this->checkpoint;
        foreach ($tmp as $k=>$v){
          $this->$k=$v;
        }
        unset($tmp);
      }
      break;
  }

}
That's the function in the parent that's causing grief (I didn't write this... it's part of a freely available pdf class).

The actual line that's cusing the main issue is this....

Code: Select all

$data = get_object_vars($this);
Is there any way I can stop it seeing the properties in my extension since this is why it's trying to access them :(

I can't seem to find an alternative function so I may end up having to write a filter.... but then I hit a hurdle that if I try to obtain all properties in my extension, I get all the inherited ones too :(

Grrrrr....

Posted: Thu Nov 17, 2005 6:18 am
by Chris Corbyn
Well a workaround has been to find all the private properties in my object and filter them out.

There's no standard function to get a list of all private properties in an object so I wrote one (it's not an ideal solution since it uses a preg_ but it does work):

Code: Select all

function get_private_properties($obj)
{
    $obj_dump  = print_r($obj, 1);
    preg_match_all('/^\s+\[(\w+):private\]/m', $obj_dump, $matches);
    return $matches[1];
}