You are here: Re: Creating a class and reusing variables... « PHP SQL « IT news, forums, messages
Re: Creating a class and reusing variables...

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 :)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация