Welcome Guest, Not a member yet? Register   Sign In
Common Practice -OR- Bad Practice (Controller/Library Interaction)
#1

[eluser]haydenp[/eluser]
How do your Controllers and Libraries interact? Below is an example (not tested but provides an idea) of what I think is not the best approach but is sometimes the easiest approach.

What are your views on having some Libraries set the properties of the Controller and other Libraries call the properties of the Controller. Is this Common Practice -OR- Bad Practice?

Below is a basic example of what I am referring to ... I'd love to get a better understanding of how other developers allow their Controllers and Libraries interact with each other.

www.my_websites.com/my_controller/my_page

CONTROLLER.

Code:
class My_controller extends Controller
{
    public $controller_property_one;
    public $controller_property_two;    
    public $results_property;
    
    public function __Construct()
    {
        parent::Controller();
                
        $this->some_method();
    }
    
    public function my_page()
    {
        $this->load->library('library_example_one');        
        $this->load->library('library_example_two');
        
        $this->library_example_one->prop_one = $this->controller_property_one;
        $this->library_example_one->prop_two = $this->controller_property_two;
        $this->library_example_one->method_one();
                
        $result = $this->library_example_two->method_two();
        $output = 'header' . $result . 'footer';
        
        echo $output;
    }
    
    public function some_method()
    {
        // Do some stuff here then set some class properties
        
        $this->controller_property_one   = 'can be a string, array, database results. etc.';
        $this->controller_property_two   = 'can be a string, array, database results. etc.';
        $this->controller_property_three = 'can be a string, array, database results. etc.';
    }
}

LIBRARY ONE.

Code:
class Library_example_one
{
    public $CI;
    public $prop_one;
    public $prop_two;
    
    public function __Construct()
    {
        $this->CI =& get_instance();                
    }
    
    public function method_one()
    {
        // Do some stuff here then set the controllers class properties
        
        $x = $this->prop_one . $this->prop_two;
        $this->CI->results_property = $x;                
    }
}

LIBRARY TWO.

Code:
class Library_example_two
{
    public $CI;
    
    public function __Construct()
    {
        $this->CI =& get_instance();                
    }
    
    public function method_two()
    {
        // Using the value in the controllers class property 'results_property', do something
        // then return the new result
        
        $z = $this->CI->results_property;

        return $z . $z . $z;                
    }
}


Messages In This Thread
Common Practice -OR- Bad Practice (Controller/Library Interaction) - by El Forum - 01-26-2010, 06:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB