1

I'm trying to access protected variables in a second-child class extended from its parent, but every time I try to access them, they are NULL.

What's weird to me is that I can access the parent's protected functions without a problem (eg. $this->_submit in the second child class.) I checked the parent class and the variable are set there, so I'm sure it's something dumb I am missing (still learning OOP). Maybe something to do with the constructor? But if I call parent::__construct() in the second child, it throws an error because the config details are missing?

Parent

<?php defined('SYSPATH') or die('No direct script access.'); abstract class Rimage { protected $_config; protected $_service; protected $_client; public static function instance($config, $service) { return new Rimage_Client($config, $service); } public function __construct($config = array(), $service = NULL) { $this->_config = $config; $this->_service = $service; $this->_client = new SoapClient('url'); } } ?> 

First Child

<?php defined('SYSPATH') or die('No direct script access.'); class Rimage_Client extends Rimage { protected $_caller; public function __construct($config = array(), $service = NULL) { parent::__construct($config, $service); $this->_caller = Arr::get($config, 'caller', array()); } public function get($id = NULL) { return new Rimage_Job_Status($id); } protected function _submit($options, $request_class) { $job->request = $options; $response = $this->_client->$request_class($job); /** Client is undefined??**/ return $response; } } // End Rimage_Client ?> 

Second Child

<?php defined('SYSPATH') or die('No direct script access.'); class Rimage_Job_Status extends Rimage_Client { public function __construct($id) { return $this->_retrieve($id); } private function _retrieve($id = NULL) { $options->CallerId = $this->_caller; /** $_caller is undefined??? **/ $options->JobId = $id; $response = $this->_submit($options, 'test'); return $response->whatever; } } // End Rimage_Job_Status ?> 

Calling the code with Rimage::instance($config,'job')->get('12345');

Edit:

The error I'm getting is that $_client is NULL in the child, but not in the parent... $_caller is NULL in the second child.

Cheers and merry christmas!

5
  • I'm working mostly with enterprise Java and mobile applications and can't say what is wrong with your code but I think you should prepare some simple code snippet in order to demonstrating here so that you can get the desired answer as soon as possible. It may somewhat be difficult to go through such a lengthy code for tracing. I think. May not it be? Commented Dec 25, 2011 at 3:32
  • What error? We need to guess? Also - return in constructor makes no sense Commented Dec 25, 2011 at 3:32
  • Sorry, added an edit that describes the error, I also added the error described in comments in the code. Commented Dec 25, 2011 at 3:37
  • Making a parent class depend on a child class is a bad class structure. Also, could you possibly cut this example down to the relevant parts and specify exactly where the error occurs? Commented Dec 25, 2011 at 4:07
  • Okay, hopefully that looks a little better. Commented Dec 25, 2011 at 4:22

2 Answers 2

2

The __construct() function is not inherited to subclasses, so there is no reason for $this->_caller to be set in the second child class. To execute the __construct function of the parent, you need to invoke parent::__construct() in the __constructor of the child.

Sign up to request clarification or add additional context in comments.

6 Comments

okay, but if I try to construct the parent, it is asking me to provide the configuration details again. Surely I shouldn't have to provide them with every construct? Also, how am I able to access the parent function _submit() without constructing the parent?
I dont get you. But what I'm saying is that, in order to inherit the __construct function to the child you must explicity do it, and it is done in the way I told you before: invoking parent::__construct() in the child's __construct(). It doesn't initialize a parent instance, but just "calls" the the function to be executed on the child class.
So just pass the $config parameter: parent::__construct($config). Please let me know if it worked.
I'm going to do some more reading on constructors and report back.
@DaveKiss: Of course you have to provide the configuration for every constructor! The point is your parent constructor requires it and so the child class has to provide it. If it doesn't have the knowledge to construct it by itself then it should of course be passed with the child's constructor and then bubbled up to the parent constructor.
|
0

When you construct a new Rimage_Job_Status, the Rimage_Job_Status::__construct function is executed. The only thing it does is to invoke Rimage_Job_Status::_retrieve(). In Rimage_Job_Status::_retrieve, you try to access $this->_caller. But that doesn't exist, because it has not been set anywhere in the steps that I just described.

To be honest, that's quite a jumbled way to use objects and classes. I'd recommend a complete rewrite/rethinking of what you're trying to do here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.