Jump to content

Is there a way to automatically run a class's parent's construct method


Recommended Posts

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?

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*/}

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.

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

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()

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.