| 
 Posted by Paul on 05/26/05 09:53 
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. 
>  
sorry just a quick note I caught myself on... 
 
inside the object you'd have to declare the variable...  like so.. 
 
class Cart { 
   var $var; 
 
   function Cart() { 
     $this->var =& $GLOBALS['var']; 
   } 
 
} 
 
for php 5 you can use its "native" constructor __Construct() instead of  
the shown example... which is for PHP < 5 :)
 
[Back to original message] 
 |