Welcome Guest, Not a member yet? Register   Sign In
$this->load->library and __construct
#11

[eluser]pistolPete[/eluser]
[quote author="GSV Sleeper Service" date="1236093696"]If it were a single string then you could only ever make constructors that accept a single parameter. By using an array you can pass an unlimited amount of parameters.[/quote]

I disagree.
Code:
function library($library = '', $params = NULL, $object_name = NULL)
{
        if ($library == '')
        {
            return FALSE;
        }

        if ( ! is_null($params) AND ! is_array($params))
        {
            $params = NULL;
        }
If the check ! is_array($params)) wasn't there, you could easily pass everthing to a library constructor: a string, an array, an object...
Because later on, the class is instantiated using:
Code:
// $config == $params from above
$CI->$classvar = new $name($config);
#12

[eluser]xwero[/eluser]
I agree with pistolPete, the array check is unnecessary limiting. Why should you have to wrap an array around a single constructor parameter?
#13

[eluser]Aniket[/eluser]
Hi all,
Passing an array with required values while loading the library, model has an advantage....say you have more than 10 variables in you model/library and you want to initialize them when loading the model/library. You don't have to pass so may parameter while loading ...your code will look lengthy....so i will say clubbing the parameters together and passing them is a better option...from my point of view.
#14

[eluser]xwero[/eluser]
Aniket Tobz/my question is not if the library method should pass an array or individual parameters but why the params parameter is restricted to an array.
Code:
// now
$config = array('my_key');
$this->load->library('house',$config);
// more developer friendly
$this->load->library('house','my_key');

It's not even a problem if you put the default parameter in a config file but then you have to write it as follows
Code:
$config = 'my_key';
#15

[eluser]Aniket[/eluser]
ohh....thanx for putting me on right track :-)
i read on the net that in php4 array are more efficient than objects but has no advantage php5. And as CI supports php4 and 5 maybe that was the reason to use array.
Correct me if wrong.
Thanks in advance
#16

[eluser]xwero[/eluser]
the params parameter just passes its content to the library that gets loaded. The only thing that can go wrong is when a developer is too lazy to type NULL if a custom object name needs to be set.
Code:
$this->load->library('home','','myhome');
But this can be prevented by using
Code:
if( ! is_object($params) && empty($params)) { $params == NULL; }
I assume 0 won't be passed to the constructor and adding an empty object would make no sense either.




Theme © iAndrew 2016 - Forum software by © MyBB