Posted by Paul on 05/26/05 09:50
Bonge Boo! wrote:
> I hope this is an obvious question, but I'm very new to classes.
>
> I have created a class that have a number of variables defined for it.
>
> class cart {
>
> var $username = "blah";
> var $password = "blahblah";
> var $database = "zippy";
> var $hostname = "127.0.0.1";
>
> Etc...
>
> It suddenly struck me that it would make more sense to have these variables
> stored in another php include so that other functions and scripts can use
> these values without them being duplciated.
>
> However I cannot find out how to pull my "global" variables into my class.
>
> I believe I could make the values constants with
>
> Define('USERNAME' , 'blah');
>
> And simply use that a constant my class. But that also means going through a
> whole load of code and changing my $username to USERNAME and probably
> buggering something up there.
>
> So if anyone can point the way to getting variables into a class from
> "outside" I would appreciate it.
>
There's a few ways to do it... probably simplest way for you would be
to do something like this inside your class..
$var =& $GLOBALS['var']; // this will only create a reference to the
global variable $var inside your object. If you want a copy take out the
& ($var = )
hope that helps..
btw if you have any variable that you need to "change" and its too much
hasle... sometimes a good workaround is this exact "trick" ...
$oldVar =& $NEW_VAR; doesn't take up any new resources as you're only
creating a "pointer" not a duplicate ..
[Back to original message]
|