Welcome Guest, Not a member yet? Register   Sign In
Another 'best practice' question about controllers (and PHP classes in general)
#1

[eluser]Bleeding Edge Productions[/eluser]
Hi guys,

Apologies for another best practice question. Assuming:

Code:
class Homepage extends MY_Controller {
    
    /**
     * Set page specific variables
     */
    protected $page_id             = 'pageidhere';
    protected $parent_id           = 'parentidhere';
    protected $page_title          = 'Page Title Goes Here';
    protected $h1_text             = 'Heading 1 Text';
    protected $page_color          = '#fff000';
    protected $bg_position         = '50px 200px';
    protected $h1_margins          = '60px 0px 40px 400px';
    protected $meta_description    = 'Meta description here.';
    protected $meta_keywords       = 'keywords, go, here';
    protected $meta_robots         = 'INDEX,FOLLOW';
    
    // --------------------------------------------------------------------
    
    /**
     * Constructor - inherits site-wide variables from MY_Controller.
     */
    public function __construct() {
        
        parent::__construct();
        
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Generate the page
     */
    function index(){
        
        /**
         * Assign variables to the $pagevars array
         */
        $pagevars['base']                = $this->base;
        $pagevars['assets']              = $this->assets;
        $pagevars['page_id']             = $this->page_id;
        $pagevars['parent_id']           = $this->parent_id;
        $pagevars['page_title']          = $this->page_title;
        $pagevars['h1_text']             = $this->h1_text;
        $pagevars['page_color']          = $this->page_color;
        $pagevars['bg_position']         = $this->bg_position;
        $pagevars['h1_margins']          = $this->h1_margins;
        $pagevars['meta_description']    = $this->meta_description;
        $pagevars['meta_keywords']       = $this->meta_keywords;
        $pagevars['meta_robots']         = $this->meta_robots;
        
        /**
         * Load the view
         */
        $this->load->view('homepage', $pagevars);
        
    }
    
}

Is it best to set the variables as protected variables outside the index() function (as I have done here), rather than directly in $pagevars, or does it make no difference? I only have one page being rendered per controller, so there is no need to have the variables per-function.

Cheers!

Martin
#2

[eluser]jrtashjian[/eluser]
Good practice in php when defining variable visibility in a class is to make all vars private or protected. Relax restrictions only as needed. Differences between property visibility:

Public properties can be accessed from anywhere.
Private properties can only be accessed from within the enclosing class. Subclasses have no access.
Protected properties can only be accessed from within the enclosing class and from a subclass.

Yes it's better to define those variables at the top of the controller. Bad practice would be repeating yourself. I do something similar to what you've posted. I'd say you're on the right track.
#3

[eluser]Bleeding Edge Productions[/eluser]
Thanks JR!

Martin




Theme © iAndrew 2016 - Forum software by © MyBB