CodeIgniter Forums
Preventing DRY in OOP - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Preventing DRY in OOP (/showthread.php?tid=60071)

Pages: 1 2 3


Preventing DRY in OOP - El Forum - 01-11-2014

[eluser]ivantcholakov[/eluser]
@behnampmdg3

This is what I would do:

Code:
class Base_Class extends CI_Controller { // Or MY_Controller

    public function __construct() {

        parent::__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:
        $this->registry
            ->set('page_title', "My Account - ")
            ->set('ad_without_photo_found', $ad_without_photo_found)
        ;

        // An alternative way for setting:
        $this->registry->set(array(
            '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:
        $this->registry->set(compact(
            'my_haves',
            'need_membership',
            'notifications',
            'show_contact_details',
            'update_message',
            'need_membership',
            'unread_menu_message'
        ));

    }

}

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

Code:
// 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:

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



Preventing DRY in OOP - El Forum - 01-11-2014

[eluser]behnampmdg3[/eluser]
@ivantcholakov


I am not using library. I am using MY_Controller.

In child, at the moment I do this:
Code:
class Account extends MY_Controller {
public funciton index()
{
$data['id']=$this->id;
}
}
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.

Thanks


Preventing DRY in OOP - El Forum - 01-12-2014

[eluser]InsiteFX[/eluser]
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.