0

So I have been thinking about this for a while and wondering if my init function is even working at all. This stems from the idea of object initialization in Zend Framework.

Lets Assume I have a base class like such:

class Base{
    public function __construct(){
        $this->init();
    }

    public function init(){}
}

Now lets assume I have a class from this that needs to accept a options param in the constructor. Why you wouldn't use the init function in the parent class I have no idea, but this example is based off of real code that I can't share.

class ExtendsBase extends Base{

    private $_options;

    public function __construct($options){
        parent::__construct();
        $this->_options = $options;
    }

    // Why even instantiate this if never using it?
    public function init(){
     parent::init();
    } 
}

So from there we need to extend from this and actually populate init with some things.

class EchoHello extends ExtendsBase{

    public function init(){
        parent::init();
        echo "hello"; exit;
    }
}

And then we should be able to do this:

$someVar = new EchoHello(array());
// Instantiate should echo hello and exit.

The issue is that echo "hello"; exit; is never executed. Is there something wrong with the inheritance chain?

LogicLooking
  • 916
  • 1
  • 16
  • 32
  • What you said should work but there are some syntax errors. `class EchoHello extends ExtendBase`, there's no `ExtendBase` class, it's `ExtendsBase` and in `private _options;` you are missing something: `private $_options;` – pNre Oct 09 '13 at 16:59
  • Apologizes for this, I wrote this up right here. Sorry for the syntax errors. I was confused if this type of inheritance was working or not. – LogicLooking Oct 09 '13 at 17:02
  • I didn't try it but I'm sure it works as you intended (without the errors of course), maybe your problem with the original code is somewhere else – pNre Oct 09 '13 at 17:04
  • I Have updated the code to remove the errors if any one can try it ... The original code is very similar set up. and when I try to echo something in it's init it doesn't work. – LogicLooking Oct 09 '13 at 17:09
  • https://eval.in/53414 – pNre Oct 09 '13 at 17:11

1 Answers1

0

Appranetly this piece of code executes as it should. as provided by This eval script run.

LogicLooking
  • 916
  • 1
  • 16
  • 32