CodeIgniter Forums
$this->load->library and __construct - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: $this->load->library and __construct (/showthread.php?tid=11423)

Pages: 1 2


$this->load->library and __construct - El Forum - 09-08-2008

[eluser]matt2012[/eluser]
im using a class that has a __construct

Code:
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,

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.


$this->load->library and __construct - El Forum - 09-08-2008

[eluser]Pascal Kriete[/eluser]
$this->load->library instantiates the class and saves a reference as $this->class_name (basically creating a singleton).

What you can do is this:
Code:
$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.


$this->load->library and __construct - El Forum - 09-08-2008

[eluser]Daniel Walton[/eluser]
[quote author="inparo" date="1220906783"]Your constructor will get that array as it's first parameter.[/quote]

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


$this->load->library and __construct - El Forum - 09-08-2008

[eluser]Pascal Kriete[/eluser]
"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).


$this->load->library and __construct - El Forum - 09-08-2008

[eluser]matt2012[/eluser]
Thanks guys thats great!


$this->load->library and __construct - El Forum - 09-08-2008

[eluser]Daniel Walton[/eluser]
[quote author="inparo" date="1220907215"]"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).[/quote]

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.


$this->load->library and __construct - El Forum - 09-08-2008

[eluser]Colin Williams[/eluser]
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.


$this->load->library and __construct - El Forum - 03-02-2009

[eluser]Tobz[/eluser]
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


$this->load->library and __construct - El Forum - 03-02-2009

[eluser]Colin Williams[/eluser]
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.


$this->load->library and __construct - El Forum - 03-03-2009

[eluser]GSV Sleeper Service[/eluser]
[quote author="Tobz" date="1236071299"]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[/quote]

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.