|
Posted by Oli Filth on 11/03/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
[Back to original message]
|