Setting up a init function and function inheritance.

Discussion in 'Web Design and Development' started by Cabbit, Mar 16, 2010.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    I have been working on a simple php mvc framework and its going pretty well i have just been given permission to integrate a ActiveRecord class into it and have redone some routing and such but one thing that is bugging me is a better way to calling the show() function for templates.

    Just now a view is called like so

    PHP:
    <?php
    # Author:   jerryrjroy
    # Name:     aboutController
    # Version:  1.0

    Class indexController Extends Cabbit_Controller_Action
    {
        
    # Initialisation function
        
    private function init()
        {
            
        }

        
    # /index/
        # /index/index
        
    public function index()
        {
           return 
    $this->Cabbit->Template->show();
        }
    }
    And well i would rather have it not need a return or perhaps some logic to handle all the returns in the init() function(which is brand new to the controller templates and i have not sorted its full porpoise yet in the code).

    So what i be asking is if its possible to call a return like this without writing it into the function.

    The show is populated in the template by the class name = view folder, action name = view file, class name present = layout template.


    Maybe something like this but without having to write the $this->init part.

    PHP:
    Class indexController Extends Cabbit_Controller_Action
    {
        
    # Initialisation function
        
    public function init()
        {
            return 
    $this->Cabbit->Template->show();
        }

        
    # /index/
        # /index/index
        
    public function index()
        {
            
    $this->init();
        }
    }
     
  2. NoNameBrand macrumors 6502

    Joined:
    Nov 17, 2005
    Location:
    Halifax, Canada
    #2
    Not really, no.

    If the thing calling $object->index() is expecting something out of that function call, you should feel obligated to provide it, or modify the caller to not expect something.

    You could have an instance variable that stores the result of the ->show() call made in init() and then return that in the ->index() call.

    Personally, I dislike the $this->Cabbit->Template->show() chain - I would have $this->Cabbit->foo() which in turn called $this->template->show(), as it gives more leverage in refactoring. If Cabbits aren't something you control, well, go for it.
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    I'll shorten some of the calls that are made for now i have made it so that the only init() is needed to be called.
    This means that you can set in init for it to load the default layout or a specific layout. The 3rd means is if a layout exists that matches the name of the controller it is loaded up.

    It is rather a shame i have not been able to work out a way to call up the view without telling it to return the layout from the function. It would be much neater and more elegant if i could program the system to load up the layout without telling it too.

    PHP:
    Class indexController Extends Cabbit_Controller_Action
    {
        
    # Initialisation function
        
    private function init()
        {
            return 
    $this->Cabbit->Template->layout();
        }

        
    # /index/
        # /index/index
        
    public function index()
        {

            
    # Load Init
            
    $this->init();
        }
     
  4. NoNameBrand macrumors 6502

    Joined:
    Nov 17, 2005
    Location:
    Halifax, Canada
    #4
    Why not put it in the constructor?

    PHP:
    Class indexController Extends Cabbit_Controller_Action
    {
        public function 
    __construct()
        {
           
    parent::__construct();
           
    $this->Cabbit->Template->layout();
        }

         
    //...
     
  5. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #5
    Ok so what would a __construct do then?

    If i go for

    PHP:
    Class indexController Extends Cabbit_Controller_Action
    {
        
        public function 
    __construct()
        {
           
    parent::__construct();
           
    $this->Cabbit->Template->layout();
        }

        
    # /index/
        # GET List
        
    public function index()
        {

        }
    Code:
    Warning: Missing argument 1 for Cabbit_Controller_Action::__construct(), called in /Users/jerry/Sites/workspace/Cabbit/application/controllers/indexController.php on line 11 and defined in /Users/jerry/Sites/workspace/Cabbit/library/Cabbit/CabbitControllerAction/CabbitControllerAction.class.php on line 13
    
    Notice: Undefined variable: Cabbit in /Users/jerry/Sites/workspace/Cabbit/library/Cabbit/CabbitControllerAction/CabbitControllerAction.class.php on line 15
    
    Notice: Trying to get property of non-object in /Users/jerry/Sites/workspace/Cabbit/application/controllers/indexController.php on line 12
    
    Fatal error: Call to a member function layout() on a non-object in /Users/jerry/Sites/workspace/Cabbit/application/controllers/indexController.php on line 12
    
    So now i need to work out what this value is it can not find.
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    The line "parent::__construct();" should only be used if the class being extended has a constructor. I'm guessing that's why the error is occurring. I don't use PHP classes very much so not positive on the error messages.
     
  7. NoNameBrand macrumors 6502

    Joined:
    Nov 17, 2005
    Location:
    Halifax, Canada
    #7
    Code:
    Warning: Missing argument 1 for Cabbit_Controller_Action::__construct(), called in /Users/jerry/Sites/workspace/Cabbit/application/controllers/indexController.php on line 11 and defined in /Users/jerry/Sites/workspace/Cabbit/library/Cabbit/CabbitControllerAction/CabbitControllerAction.class.php on line 13
    The parent::__construct() calls the parent classes' constructor; it obviously takes a parameter.

    Solution is $foo, below:
    PHP:
    Class indexController Extends Cabbit_Controller_Action
    {
        
        public function 
    __construct($foo)
        {
           
    parent::__construct($foo);
           
    $this->Cabbit->Template->layout();
        }
    I think the other errors come from the fact that it's bailing on the parent::__construct() call.


    That's true. One of many reasons to define one.
     
  8. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #8
    Whilst i did get it working nicely this way, well after a bit of tweaking. I did break a lot of the template stuff like variables were no longer passed to view files and such which is a pain.

    Maybe need to look at rewriting this bit from scratch and see if it is possible.
     

Share This Page