Welcome Guest, Not a member yet? Register   Sign In
Libraries
#1

[eluser]codelearn[/eluser]
Quick question about Libraries.

Is there a way to pass variables to the Library other than through the constructor? I have vars in the class itself that I want to set dynamically from the controller. I am just getting started building my own libraries and thus am a little confused.

Thanks all!
#2

[eluser]coolfactor[/eluser]
Here is one approach.

Code:
$this->load->library('sample');
$this->sample->var1 = 'value1';
$this->sample->var2 = 'value2';
#3

[eluser]codelearn[/eluser]
And then in the library $var2 would have value of 'value2'?
#4

[eluser]coolfactor[/eluser]
[quote author="codelearn" date="1187907068"]And then in the library $var2 would have value of 'value2'?[/quote]

The following is how you'd access var2:
Code:
$this->var2

It would be an object variable.
#5

[eluser]codelearn[/eluser]
Coolfactor:

I tried that. In the controller I put:

Code:
$this->load->library('Weather');
$this->weather->zp1 = '01810';
$d['weather_info']=$this->weather->get_weather();

In the library I put

Code:
class Weather
{
    var $zipcode = $this->zp1;                // Zipcode

And I am getting

Quote:Parse error: syntax error, unexpected T_VARIABLE in /home/cldev/public_html/includes/application/libraries/Weather.php on line 40

Line 40 is var $zipcode = $this->zp1;

Any ideas? Thanks.
#6

[eluser]coolfactor[/eluser]
You need to study up on object-oriented programming a bit more. Wink

In your controller, just assignn the "zipcode" directly:

Code:
$this->weather->zipcode = '01810';

No need to pass it through a different variable.
#7

[eluser]codelearn[/eluser]
Thanks! Just starting to make libraries.




Theme © iAndrew 2016 - Forum software by © MyBB