You are here: Re: Global Vars « All PHP « IT news, forums, messages
Re: Global Vars

Posted by Michael Fesser on 12/01/06 15:23

..oO(Adrián Ribao Martínez)

>Hello, I have to create an instance of a class that should be accessible
>from all the other classes.
>What do you recommend me?
>- Using globals?
>- Pass the instance as an argument and load it in a __constructor?
>- Something different?

A singleton, e.g. (assuming PHP5)

class TMyClass {
private static $instance = NULL;

public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}

...
}

Then wherever you need that instance, you just have to get it:

$foo = TMyClass::getInstance();

HTH
Micha

 

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

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