• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preventing DRY in OOP


This is what I would do:

class Base_Class extends CI_Controller { // Or MY_Controller

    public function __construct() {


        $this->load->library('registry'); // Or autoload it.

        // Load othel libraries, models, etc.

        // Retrieve repetitive used data by using your libraries and models.

        // And then set it within the Registry:
            ->set('page_title', "My Account - ")
            ->set('ad_without_photo_found', $ad_without_photo_found)

        // An alternative way for setting:
            'unread' => $unread,
            'logged_user' => $logged_user,

        // Or if you have all local variables with data retrieved and
        // you intend to use their names as indexes:



Then within the ancestor controllers or views I would access needed data this way:

// Example 1
$title = $this->registry->get('title);

// Example 2, an array of values is returned.
$user_status = $this->registry->get(array('unread', 'logged_user'));

// Example 3, why not just extract the values.
extract($this->registry->get(array('unread', 'logged_user')));

// Example 4, you are within a model, and you need access there too:
// I assume that the registry library has been already loaded:
$ci = get_instance();
$need_membership = $ci->registry->get('need_membership');

// Got it?

An amendment, using the helper, if it has been loaded:

<h1>&lt;?php echo registry('title'); ?&gt;</h1>


I am not using library. I am using MY_Controller.

In child, at the moment I do this:
class Account extends MY_Controller {
public funciton index()
I need to do something that I wnt have to $data['id']=$this->id; in children and instead I just have the $data['id'] set and ready to use.


If you use either ivantcholakov's or my Registry then you should not have any problems, set the registry with your data then use the registry methods to call back the values.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.