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

[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.
#2

[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.
#3

[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.
#4

[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).
#5

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

[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.
#7

[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.
#8

[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
#9

[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.
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB