Reply to Re: Global Vars

Your name:

Reply:


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

[Back to original 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

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