0

I'm experimenting with MVC trying to make a simple framework. This is an example of what I'm doing:

<?php
require_once('config.php'); //Here I have the Config object
class App{
    protected $config;
    protected $controller;
    public function init(){
        $this->config = new Config;
        $this->controller = new Main_Controller;
    }
}
class Main_Controller extends App{
    public function __construct(){
        var_dump($this->config);
    }
}
$app = new App;
$app->init();

The problem is that my var_dump is returning NULL, so why isn't Main_Controller reading that App property?

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
enzo
  • 624
  • 6
  • 12

1 Answers1

0

Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).

Paul Sullivan
  • 2,865
  • 2
  • 19
  • 25
  • Mmmm right, the problem is that my App class doesn't have a constructor, so I'm pretty sure I shouldn't use parent::__construct(); in my Main_Controller class since that function doesn't exist. Also I don't think it has something to do with the constructor because if I put the var_dump in a classic function inside Main_Controller and then call it from App right before instantiating Main_Controller, I'm still getting NULL for all the App properties... – enzo Jul 31 '13 at 01:41
  • The text I posted is taken directly from the php5 documentation verbatum and is the only way I can think that instance methods may not be initialised. Php'ers out there can anyone clarify – Paul Sullivan Jul 31 '13 at 06:53