Question

I keep an abstract class definition in a directory called /classes. It looks like this:

abstract class baseController {

    protected $registry;

    //Obtain registry object
    protected function __construct($reg) {
        $this->registry = $reg;
    }

    protected function show() {

    }

}

In the /controllers/ directory (they share the same parent folder) I have my specific controller class files. For instance,

class indexController extends baseController {

    public function show() {

        //render index page
        $this->registry->view->render('index');

    }

}

I use two spl_autoload_register() calls in my config script to make sure that both directories are found when classes are needed - but when I try to instantiate indexController from my router.class.php file (which is in the /classes/ directory) I get the following error:

PHP Fatal error:  Call to protected baseController::__construct() from context 'router'...

It's identifying the line: $this->controller = new indexController($this->registry); as the culprit.

The __construct is protected, but indexController should be inheriting from baseController and therefore, since the __construct method isn't redefined, it should be calling the parent classes constructor. Or so I thought.

What the heck is going on?

Was it helpful?

Solution

You should use public definition in __construct method, since you instantiate the class outside the class context (ex in Router class).

This may work if you instantiate it inside another class that extends baseController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top