ttocskcaj Posted August 14, 2011 Share Posted August 14, 2011 I'm working on a MVC website, At the moment my controller class has the constructor function __construct(){ $view = new Smarty(); } Every time I make a new controller extending this class I have to do this function __construct(){ parent::__construct(); } so that the view object is created. Is there anyway to do that automatically without having the parent::__construct() line? Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/ Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 I don't see any other way, if someone knows enlighten me also.. Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257020 Share on other sites More sharing options...
ignace Posted August 14, 2011 Share Posted August 14, 2011 Leave the constructor out of the child class. It will automatically call the parent constructor. Use the below trick if you for some reason still would need to overwrite the parent constructor: public function __construct() { $this->view = new Smarty(); $this->init(); } public function init() {/*overwrite this method in the child class*/} Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257030 Share on other sites More sharing options...
ttocskcaj Posted August 14, 2011 Author Share Posted August 14, 2011 Removing the constructor on the child class gives Call to a member function assign() on a non-object Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257035 Share on other sites More sharing options...
trq Posted August 14, 2011 Share Posted August 14, 2011 Can we see the relevant code? Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257036 Share on other sites More sharing options...
ignace Posted August 14, 2011 Share Posted August 14, 2011 Removing the constructor on the child class gives Call to a member function assign() on a non-object Your view should be protected not private if you intend to use it in the child classes. Your code previously worked because you created a new public variable view on your child object in the constructor. Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257041 Share on other sites More sharing options...
ttocskcaj Posted August 14, 2011 Author Share Posted August 14, 2011 Parent is here https://github.com/ttocskcaj/TTMailer/blob/master/includes/controller.class.php child is here https://github.com/ttocskcaj/TTMailer/blob/master/public/controllers/index.php Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257136 Share on other sites More sharing options...
trq Posted August 14, 2011 Share Posted August 14, 2011 And the relevant code? Where the objects are created? Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257138 Share on other sites More sharing options...
ttocskcaj Posted August 14, 2011 Author Share Posted August 14, 2011 I've never done this MVC stuff before :s This file is supposed to take variable from the URI and create the controller object. https://github.com/ttocskcaj/TTMailer/blob/master/index.php Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257149 Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 Leave the constructor out of the child class. It will automatically call the parent constructor. Use the below trick if you for some reason still would need to overwrite the parent constructor: public function __construct() { $this->view = new Smarty(); $this->init(); } public function init() {/*overwrite this method in the child class*/} I have my brain wrapped out Sub-Classing recently...] thats the only reason i would see to call a parent's constructor, if your using a framework or had build your own... class Application(Frame): def __init__(self, master=None): Frame.__init__(self, master,height=2) # create a grid self.grid() self.createWidgets() self.grid_propagate() def createWidgets(self): return(0) app = Application() app.master.title("User Input") app.mainloop() Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257343 Share on other sites More sharing options...
ttocskcaj Posted August 15, 2011 Author Share Posted August 15, 2011 phpSensei, I don't follow you. I'm just going to assume there's no simple way to do this? I should probably just use an MVC framework Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257505 Share on other sites More sharing options...
ttocskcaj Posted August 15, 2011 Author Share Posted August 15, 2011 I made it work. It works when you don't declare the view property at the start of the class. So instead of class controller { public $view; __construct(){ $view = new Smarty(); } } just do class controller { __construct(){ $view = new Smarty(); } } It doesn't work if you make $view private, or protected either. You just have to leave it out. Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257537 Share on other sites More sharing options...
trq Posted August 15, 2011 Share Posted August 15, 2011 But then $view is not accessible outside of the __construct. Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257540 Share on other sites More sharing options...
ttocskcaj Posted August 15, 2011 Author Share Posted August 15, 2011 sorry, that should have been $this->view Quote Link to comment https://forums.phpfreaks.com/topic/244733-is-there-a-way-to-automatically-run-a-classs-parents-construct-method/#findComment-1257563 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.