CodeIgniter Forums
Libraries - 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: Libraries (/showthread.php?tid=2770)



Libraries - El Forum - 08-23-2007

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


Libraries - El Forum - 08-23-2007

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

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



Libraries - El Forum - 08-23-2007

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


Libraries - El Forum - 08-23-2007

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


Libraries - El Forum - 08-23-2007

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


Libraries - El Forum - 08-23-2007

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


Libraries - El Forum - 08-23-2007

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