CodeIgniter Forums
Way to define $data array for all functions inside a class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Way to define $data array for all functions inside a class (/thread-21817.html)



Way to define $data array for all functions inside a class - El Forum - 08-21-2009

[eluser]Rost[/eluser]
Is there a way so I can predefine a data array? Because in ever function I make I have to define the same data array(like site title, and no I don't want them hard-coded in my script)

Regards

Rost


Way to define $data array for all functions inside a class - El Forum - 08-21-2009

[eluser]pistolPete[/eluser]
Use a class variable:

Code:
class Some_controller extends Controller {

    var $default_data = array('title' => 'some title', 'color' => 'red');

     function Some_controller()
    {
        parent::Controller();
        print_r($this->default_data());
    }
}

Or setup a custom config file: http://ellislab.com/codeigniter/user-guide/libraries/config.html


Way to define $data array for all functions inside a class - El Forum - 08-21-2009

[eluser]gwelter[/eluser]
pistolPete sounds right on. I was doing the same data preparation for certain header data across multiple classes, and I ended up writing a helper function for it called header_prep(). When I call the header, I simply use array_merge:

Code:
$this->load->view('header', array_merge(header_prep(), 'page_title' => 'My Page'));

This wouldn't apply to static data in a single class, but I thought I'd mention it anyway.