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

Posted by Oli Filth on 11/01/01 11:32

Thomas Mlynarczyk said the following on 20/11/2005 10:53:
> Also sprach Weird-beard:
>
>
>>there are situations that you must have an instance, an instance in
>>which you can fiddle with class variables. In static functions, you
>>cannot change the class variables,
>
>
> But in PHP5 I can have static class variables and I can access those with
> static functions - or have I misunderstood the manual?
>

There are indeed static class variables.

However, semantically, static variables imply variables that represent
the state of the class as a whole, not one particular instance of it.

An example of a good use of static variables might be instance counting;
i.e. every time an instance of a class is constructed, the constructor
increments a static variable. Every time an instance is destructed, the
destructor decrements the static variable. If the count reaches zero,
some other processing might occur, e.g. close a shared DB connection.

So yes, static variables and functions will do the same job as a single
instantiation, but it would really be using the wrong tool for the job.
Part of the paradigm of good OOP is making your code a semantic
representation of your data structure (that's the whole reason behind
public, protected, private, for example).

Another problem is that whilst you can pass a class instance around
(i.e. function arguments, return variables, and serialisation), you
cannot "pass" a class. You can fudge it by passing the class name as a
string and using eval() or some-such silliness, but then your code
really starts looking messy and hacky.


--
Oli

 

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

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