0

I have tried adding just an empty __construct method to a module controller.

Fatal error: Call to a member function get() on a non-object in /var/www/rodebutik.dk/public_html/wb/vqmod/vqcache/vq2-system_engine_controller.php on line 16

Line 16:

public function __get($key) {
    return $this->registry->get($key);
}
Stromgren
  • 1,064
  • 1
  • 16
  • 35
  • I don't see any reason why You shouldn't use `__construct()` methods... Please, provide us with the code, an **empty** `__construct()` cannot produce such an error. – shadyyx Jan 09 '14 at 12:48
  • 4
    I would guess that the Registry instance is passed via the the object you're extending. When you redefine and empty construct, it's no longer set - hence the error. – Anthony Sterling Jan 09 '14 at 13:02
  • https://github.com/opencart/opencart/blob/master/upload/system/engine/controller.php#L5 – Anthony Sterling Jan 09 '14 at 15:04

3 Answers3

8

The most likely explanation: the original constructor was setting up the $this->registry object. You are overriding the constructor, which now does not set up that object anymore. Hence: you error out when the code is trying to use the expected object which is not there.

If you override a constructor, you should/need to invoke the original one as well:

public function __construct(...) {
    parent::__construct(...);

    // new code
}
deceze
  • 510,633
  • 85
  • 743
  • 889
2

You can make a constructor in controller or model like this way.

class ModelMyModel extends Model {
   public function __construct($params) {
      parent::__construct($params);
   }
}
Zaheer Babar
  • 1,636
  • 1
  • 15
  • 17
2

You can use constructor like this, same for Model also you can use, by passing parameter.

class ControllerCommonDashboard extends Controller {

    public function __construct($params) {
        parent::__construct($params);
    }
}
Vijaysinh Parmar
  • 897
  • 1
  • 15
  • 19