0

I am attempting to access the parent class __construct properties within a child class that extends this, however not sure how to do this as I have tried multiple methods and didn't give me the expected result.

So I have a baseController and a indexController which extends it, I want to be able to have direct access to the properties of the parent within the child controller.

$config = ['site' => 'test.com'];

class baseController {

    public function __construct($config){

        $this->config = $config;

    }

}

class indexController extends baseController {

    public function __construct(){
        parent::__construct(); // doesnt seem to give any outcome
    }

    public static function index() {

        var_dump($this->config); // need to access within this method

    }

}

$app->route('/',array('indexController','index')); // the route / would call this controller and method to return a response
Gruber
  • 2,196
  • 5
  • 28
  • 50
mhvvzmak1
  • 307
  • 2
  • 12
  • 1
    if you call `indexController::index` in a static context, you can't have access to `$this` – Federkun Apr 05 '16 at 21:41
  • 2
    __construct($config) is not the same as __construct() – bassxzero Apr 05 '16 at 21:41
  • How else would I access the property, with static::$config? I think that I am required to call the index method as static as part of the framework which I am using – mhvvzmak1 Apr 05 '16 at 21:42
  • I tried __construct($config) previously and still got no result so removed it as I wasn't sure why it isnt working – mhvvzmak1 Apr 05 '16 at 21:43
  • "Unlike with other methods, PHP will not generate an E_STRICT level error message when __construct() is overridden with different parameters than the parent __construct() method has." http://php.net/manual/en/language.oop5.decon.php – bassxzero Apr 05 '16 at 21:43
  • Since index is a static method the constructor doesn't get called prior to calling it. Furthermore you will get an error for using `$this` in a static context, and another for not supplying `$config` argument to `parent::__construct`. Can you add the usage case to your example – Dan Apr 05 '16 at 21:50
  • I have added the full use of my app so that you can see in the main post – mhvvzmak1 Apr 05 '16 at 21:56
  • you are using `static`, so the context is wrong. – Gogol Apr 05 '16 at 22:04
  • The framework doesn't seem to allow me to change the method from static, I get an error "Non-static method indexController::index() should not be called statically (8192)" – mhvvzmak1 Apr 05 '16 at 22:06

1 Answers1

0

There are several issues with code you have there. You are setting up config as a global, it should be inside your BaseController and set it to public or protected:

class BaseController {
  protected $config = ...

Just like @mhvvzmak1 mentioned, your child constructor is calling the parent properly. for example you can do it like so:

 class IndexController extends BaseController {

     public function __construct(){
         $config = [];
         parent::__construct($config);
     }

and finally just like dan08 mentioned, you can't reference $this from a static method, change your index function:

public function index() {

Update

If you really want the child function to remain static as required by your framework, you make config a static function on the BaseController and call it in the child.

class BaseController {

   protected static function config() {
     return ['site' => 'mySite'];
   }
}

class Child extends BaseController {
   public static function index() {
      $config = BaseController::config();
   }
}
KDaker
  • 5,899
  • 5
  • 31
  • 44
  • The framework doesn't seem to allow me to change the method from static, I get an error "Non-static method indexController::index() should not be called statically (8192)" – mhvvzmak1 Apr 05 '16 at 22:04
  • are you calling the index function yourself? or does the framework call it for you? – KDaker Apr 05 '16 at 22:17
  • The framework does this for me when I visit the site route /, it automatically looks for the "index" method within the indexController and dislays the result of the function as that is what I assigned this specific route to: $app->route('/',array('indexController','index')); – mhvvzmak1 Apr 05 '16 at 22:26
  • can you show how you achieve that in the router? also any documentation for this? Just looked over at the documentation on the website and there is no mention of using controllers with routers. http://flightphp.com/learn – KDaker Apr 05 '16 at 22:29
  • There is a little about the use of it however it is very little and hard to miss, see: http://flightphp.com/learn#routing. class Greeting { public static function hello() { echo 'hello world!'; } } Flight::route('/', array('Greeting','hello')); – mhvvzmak1 Apr 05 '16 at 22:31
  • well in that case, you're better off creating a config.php file and loading it from the child directly instead of passing it form parent to child. – KDaker Apr 05 '16 at 22:36
  • I do have a config.php file which consists of the $config array, the $config keys can be accessed elsewhere in the application just not the controller currently. However I'd like this so that I can send certain values to my view from the config array such as the site name, I was hoping it would be possible to somehow add it to the base controller constructor and then extend the parent within the child controllers that require the config array. – mhvvzmak1 Apr 05 '16 at 22:39
  • Is it possible to inject the current config.php array into the BaseController? So that I can easily modify the settings there rather than them being hardcoded into the BaseController? – mhvvzmak1 Apr 05 '16 at 23:05