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;                
    }
}
#2

[eluser]n0xie[/eluser]
Why don't you pass the parameters to the library methods?

You can use get/setters or pass an array of values to your library methods.
#3

[eluser]haydenp[/eluser]
Thx for the reply n0xie

Passing parameters to the library methods ... get/setters ... 100% correct ... although, in my example I want to focus on Libraries interacting with Controller properties and whether or not that is good/bad practice.

Your point does bring up another 'good/bad practice'question:

Code:
// Is it better to:
//
// Eg. A)

$this->library_example_one->param1 = 'something';
$this->library_example_one->param2 = 'something';
$this->library_example_one->param3 = 'something';
$this->library_example_one->param4 = 'something';
etc.
$this->library_example_one->method_one();

// Eg. B)

$this->library_example_one->method_one($param1, $param2, $param3, $param4, $param5, $param6, $param7);

// Eg. C)

$array['param1'] = 'something';
$array['param2'] = 'something';
$array['param3'] = 'something';
$array['param4'] = 'something';
etc.
$this->library_example_one->method_one($array);
#4

[eluser]n0xie[/eluser]
Depending on the situation I am used to this:
Code:
$this->library->some_method('required parameter', 'optional parameters')
Where required parameter is usually a string, and optional is an array of optional parameters. Basically B and C into 1. I rarely use version A, except when using a setter which is a constraint to the behaviour of the library.

For example:
Code:
$this->load->library('library');
$this->library->set_debug(TRUE); // turns on logging, or returns error messages if something fails
$this->library->some_function($id, $options);

Depends on what feels natural to you I guess.
#5

[eluser]haydenp[/eluser]
I'm more of a Eg. B person ... but your mention of 'where required parameter is usually a string, and optional is an array of optional parameters.' ... I do like your idea of the 'array of optional parameters'!

Comments on Libraries interacting with Controller properties as in my initial post. Would you say:

1. My Libraries NEVER interact with Controller properties!
2. Occasionally my Libraries interact with Controller properties (but I try avoid this practice)!
3. Having my Libraries interact with Controller properties is common practice for me!
4. Other?

Thanks for the feedback! ;o)
#6

[eluser]n0xie[/eluser]
I would say my libraries never interact with Controller properties. You should try to keep your libraries as decoupled as possible. For instance, in theory you should be able to add a library to a non CI project and still be able to use it. I say in theory because most of the time, people use the CI active record class to interact with the database which is obviously not portable.

I would say direct interaction with Controller properties should be considered bad practice. If the library needs certain parameters, make them implicitly required. If your controller needs some data from the library, return the data from a library method. This way if you ever need to port it to another (non-CI) project, it should be as trivial as removing the CI superobject reference, and passing a database object to the library. (as well as rewriting your AR queries obviously but you catch my drift).
#7

[eluser]haydenp[/eluser]
Thanks again n0xie ... your feedback makes perfect sense.

Anyone else in the CI community care to add their 5c worth, please feel free to do so ;o)




Theme © iAndrew 2016 - Forum software by © MyBB