Reduce repetition when sharing data across an application I have a CodeIgniter 3 application. There is some data every page across the application needs to have.

This is the system I currently have.

Create a model function that will get the data I need

Class General_model extends CI_Model
{
    function data_init()
    {
        $data['session'] = $this-&gt;session-&gt;userdata('session');
        $data['foobar'] = $this-&gt;general_model-&gt;get_foobar();
        return $data;
    }
}


In every controller, I call this data in the __construct()

function __construct()
{
    parent::__construct();
    $this-&gt;load-&gt;model('general_model', '', TRUE);
    $this-&gt;data = $this-&gt;general_model-&gt;data_init();
}


I begin every controller function by getting the data in the scope of the function, which then gets passed to the view

public function index()
{
    $data = $this-&gt;data;
    $data['page_title'] = 'example';
    $this-&gt;load-&gt;view('templates/header', $data);
    $this-&gt;load-&gt;view('page/example', $data);
    $this-&gt;load-&gt;view('templates/footer', $data);
}


I feel this is not ideal. It feels repetitive and not dry to have $this-&gt;data = $this-&gt;general_model-&gt;data_init(); in every constructor and $data = $this-&gt;data; in every function.

It feels like it'd be better if I could declare $this-&gt;data = $this-&gt;general_model-&gt;data_init(); once in the application and have $this-&gt;data available to every function in every controllers. I'd also like to avoid $data = $this-&gt;data; at the start of every function, but I'd rather do that than have the additional 7 characters $this-&gt; hundreds of times across the codebase (perhaps that is me being lazy?).

I'm not sure how to improve either of those without breaking OOP and going global, or touching CodeIgniter core classes such as CI_Model.

Is there a way to improve how I'm sharing and moving this data?
I would not get all worked up about having to type a few extra letters when referencing a variable.  I could get worked up over possibly doubling the storage space required for your data depending on it's size. Let's walk though what PHP is doing behind the scenes here to understand:

// PHP references $this-&gt;data to $data by reference
$data = $this-&gt;data;

// since you modify $data here, PHP now copies the entirety of $this-&gt;data
// to $data and breaks reference
// you are now taking up double (plus some) storage for your data 
$data['page_title'] = 'example';


As opposed to directly modifying $this-&gt;data before making assignment to $data like shown below , which would not double memory utilization:

$this-&gt;data['page_title'] = 'example';
$data = $this-&gt;data;


Just remember that as soon as you modify an array assigned in this manner, you remove the ability for PHP to perform it's behind the scenes memory optimization. But really you should not be relying on this anyway, as IMO it is bad coding form to be relying on some hidden, undocumented and possibly subject to change optimization for your code to be working in the best manner.

Alternately, instead of associative array for data, why not use an object (even stdClass), then you are always working with reference to the object (unless you specifically clone it)? For example:

// assuming $this-&gt;data holds object
// $data is just reference to $this-&gt;data here
$data = $this-&gt;data;

// here modifying $data would modify both $data and $this-&gt;data
$data-&gt;page_title = 'example';


This also gives you the possible side benefit of being able to type hint against an object in method signatures as you are passing this data around or injecting it as dependency.