CodeIgniter Class Level Variables

April 23, 2017
Backend Frameworks PHP

As the CTO of Gittr, I’m always looking for ways to improve the way we write our software, from utilizing different features and techniques to simplifying or compacting our logic. We’ve got a pretty good flow going, but there was one thing I saw that was tripping me up: constantly defining and redefining variables.

The big thing we’re working on right now is the ‘administrator’ section of our app, which allows privileged users access to methods to change settings, view stats, add, remove, and edit partnered merchants and generally manage the application. We’ve gone through a few different generic methods of rendering views, which always looks a little something like this:

<?php
public function view($page) {
    //if the admin is logged in
    if ($this->UserModel->adminLoggedIn()) {
        //assemble the $data
        $data['user'] = $this->UserModel->getUserData();
        $data['company'] = $this->CompanyModel->getCompanyData();
        //load the view
        $this->load->view('admin/'.$page.'.php', $data);
    } else {
        //if the user isn't logged in, redirect to the login page
        redirect('admin/login.php');
    }
}
?>

We pass the $data['user'] and $data['company'] to each view to display the appropriate data and link to the right things. Things like the Company’s and User’s IDs are very important to keeping track of who did what, especially when dealing with multiple users. So these pieces of information are very important.

With these (and other variables) constantly being defined in every method, I wanted a way to not only make sure we’re keeping it consistent, but also cut down on file size. After removing every instance of the call to getUserData() and getCompanyData(), I counted just over 100 lines of code, or about 5,000 characters, saved altogether. The solution I found was variables defined on the class level.

The idea is this: each time the controller is constructed, the three variables ($user, $company, $loggedIn) are set. Then, they can be accessed within the methods to pass the data to the views. Here’s what that looks like:

<?php
class Demo extends CI_Controller {
    //declare the variables
    protected $someVar;
    protected $otherVar;

    public function __construct() {
        parent::__construct();
        /* Load in libraries, helpers, models, etc */
        //set the variables
        $this->someVar = $this->SomeModel->someFunction();
        $this->otherVar = $this->OtherModel->otherFunction();
    }

    public function index() {
        //use the variables
        $data['var'] = $this->someVar;
        $data['other'] = $this->otherVar;
        $this->load->view('view.php', $data);
    }
}
?>

I think this accomplishes two things: it keeps your code DRY (Don’t Repeat Yourself) and it cuts down on the possibility of breaking things when you change how you call a function (ex. changing the parameters passed and accepted). I think this is a pretty basic use of utilizing class-level variables in the controller and I’m really excited to find other ways to implement this idea. If you have other ideas on using this, leave a comment!

No Comments...yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

CSS Counting: What, How, and When

From January 29, 2017

After reading an article on CSS-Tricks, I learned about CSS Counting. It’s a cool property that’s arguably useful, but isn’t really accessible.

Read This Article
Next Post

Function Declaration in jQuery

From May 8, 2017

Three ways to declare functions in jQuery include the simple function, get/set, and extending the jQuery object. I cover all three in this short post.

Read This Article