You are here: Re: Classes without instances « PHP Programming Language « IT news, forums, messages
Re: Classes without instances

Posted by Dikkie Dik on 11/18/05 17:37

Thomas Mlynarczyk wrote:
> Hi,
>
> In many cases, one needs only a single instance of a class in a script.
> Since PHP5 offers class variables like MyClass::$myVar and even class
> constants, it seems to me that an instantiation is not needed. Is it
> therefore okay and good practise to use a class without instantiation? In
> that case I could regard such a class as a collection of functions and
> variables that belong together in a way and the prefixed class name could
> serve as a kind of namespace identifier

It is good practice if the instance would have no state. You'd only have
functions then. The standard example is the Math class in Java and
JavaScript, which is a collection of mathematical functions.

If the object has a state, it is better to use an instance of a class.
Lots of classes have one instance or almost-always one instance. The
backend database, for example. Although I did change database types in
the past and was glad that I could use more than one instance of a
database. There is nothing wrong with classes that are instantiated only
once. There is even nothing wrong with a class that only has an instance
for a really short time.

I don't like it, but if you want to _enforce_ the fact that there is
only one instance, google for "singleton pattern".

Best regards

 

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

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