Sh1d0w
Sh1d0w

Reputation: 9520

Extending abstract class gives strange error while accessing variables

So guys I have this basic structure :

interface IModule {
    public function _prepareActionObj ();
}

abstract class Module implements IModule {

    private $_action = null,

            $_actionObj = null,

            $_actionHandlers = array();

    public function __construct() {
        $this->_action = GetParamDefault('action', '');
        $this->_prepareActionObj();
    }

    private function _prepareActionObj () {
        $this->_actionObj = new $this->_actionHandlers[$this->_action]();
    }

}

So far nothing strange. But when I do this :

 class AdminHomeModule extends Module {
     private
         $_actionHandlers = array(
             '' => 'AdminModuleHomeAction'
         );
 }

and create new instance of AdminHomeModule I get this error

Undefined index: on line 22 in ....\interfaces\IModule.php

wich is exacly

$this->_actionObj = new $this->_actionHandlers[$this->_action]();

When I dump $this in Module constructor I get this

AdminHomeModule Object
(
    [_actionHandlers:AdminHomeModule:private] => Array
        (
            [] => AdminModuleHomeAction
        )

    [_action:Module:private] => 
    [_actionObj:Module:private] => 
    [_actionHandlers:Module:private] => Array
        (
        )

)

where as you guys can see I get two instances of _actionHandlers one for parent class and one from the current class that extends Module. I don't get why I have two _actionHandlers? Shouldn't I overwrite it when declaring it in child class, because right now _prepareActionObject is looking in the wrong place and I get this nasty error...

Upvotes: 1

Views: 329

Answers (1)

Sherif
Sherif

Reputation: 11943

That is intended behavior. A private class initializer means that only that class is allowed to access this property. This doesn't change if the class is abstract.

What you can do instead is declare the abstract class initializer and the inheriting classes as protected and that way all inheriting classes will be able to access the same property.

abstract class Module implements IModule {

    protected $_action = null,

            $_actionObj = null,

            $_actionHandlers = array();

    public function __construct() {
        $this->_action = GetParamDefault('action', '');
        $this->_prepareActionObj();
    }

    private function _prepareActionObj () {
        $this->_actionObj = new $this->_actionHandlers[$this->_action]();
    }

}

class AdminHomeModule extends Module {
    proteced
        $_actionHandlers = array(
            '' => 'AdminModuleHomeAction'
        );
}

EDIT: My mistake, I was giving advice without properly reviewing the code ...

[redacted what was here]

Upvotes: 1

Related Questions