Your constructor will get that array as it’s first parameter. If you need more instances you can use new, but in that case you’ll obviously need to pass the array manually.
Also, it has to be an array, a single value doesn’t work (I’ve run into that several times).
Good catch. The loader class docs don’t really go into much depth here. Would it not be more flexible to work by extracting first? As the case in point would require the instantiated class to be edited to expect its parameters in this way.
It would be nice if it did call_user_func_array() instead of requiring it be a single array parameter, but once you accept that it’s going to need to be an array, it’s not too hard to stick with that convention.
It can’t be a single string because the library() function of the Loader class insists that it be an array. You can disagree with the reason for doing that (I don’t know why they did it either) but it’s not really that hard to deal with.
I don’t understand why the input params have to be an array.
There doesn’t seem to be any reason why it can’t be a single string
can someone shed some light on this for me?
Thanks
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.
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.
I disagree.
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:
// $config == $params from above $CI->$classvar = new $name($config);
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.
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.
// 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
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
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.