EllisLab text mark
Advanced Search
     
$this->load->library and __construct
Posted: 08 September 2008 12:23 PM   [ Ignore ]
Joined: 2007-04-04
61 posts

im using a class that has a __construct

public function __construct($api_key$secret

the problem is the construct is being loaded and complaining about
missing variables before ive called the class
heres the code,

$api_key "fgugodsogfu";
$secret "dfgdfgdsgfdfc"
$this->load->library('something');
$something = new Something($api_key$secret); 

so the __construct class is being called even if I cross out the last line.

 Signature 

easy2web

 
Posted: 08 September 2008 12:46 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
2482 posts

$this->load->library instantiates the class and saves a reference as $this->class_name (basically creating a singleton).

What you can do is this:

$config = array(
    
'api_key'    => 'lkjiojef',
    
'secret'    => 'iopuqr'
);
$this->load->library('whatever'$config);

/* Later on */
$this->whatever->some_method(); 

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.

 Signature 
 
Posted: 08 September 2008 12:50 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-22
575 posts
inparo - 08 September 2008 04:46 PM

Your constructor will get that array as it’s first parameter.

I was under the impression it was extracted and given to the constructor as separate arguments.

 Signature 

(a.k.a the_butcher)

 
Posted: 08 September 2008 12:53 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
2482 posts

“Passing Parameters When Initializing Your Class”

Also, it has to be an array, a single value doesn’t work (I’ve run into that several times).

 Signature 
 
Posted: 08 September 2008 01:36 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-04-04
61 posts

Thanks guys thats great!

 Signature 

easy2web

 
Posted: 08 September 2008 01:59 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-22
575 posts
inparo - 08 September 2008 04:53 PM

“Passing Parameters When Initializing Your Class”

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.

 Signature 

(a.k.a the_butcher)

 
Posted: 08 September 2008 04:49 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-27
2617 posts

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.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

 
Posted: 03 March 2009 12:08 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2007-03-20
51 posts

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

 
Posted: 03 March 2009 02:03 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-27
2617 posts

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.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

 
Posted: 03 March 2009 06:21 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2008-03-13
515 posts
Tobz - 03 March 2009 05:08 AM

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.

 Signature 

:wq

 
Posted: 03 March 2009 07:32 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-17
1073 posts
GSV Sleeper Service - 03 March 2009 11:21 AM

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); 
 
Posted: 03 March 2009 08:03 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

I agree with pistolPete, the array check is unnecessary limiting. Why should you have to wrap an array around a single constructor parameter?

 
Posted: 03 March 2009 09:24 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-19
66 posts

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.

 Signature 

Aniket

 
Posted: 03 March 2009 09:39 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

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

$config 'my_key'
 
Posted: 03 March 2009 09:51 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-19
66 posts

ohh….thanx for putting me on right track grin
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

 Signature 

Aniket

 
Posted: 03 March 2009 10:19 AM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

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.

$this->load->library('home','','myhome'); 

But this can be prevented by using

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.