|
Posted by Henk verhoeven on 03/23/07 23:49
peter schreef:
>> Why use :: instead of -> then? hmm... I dunno, it's kewl? I've only used
>> it once with a singleton where it seemed to make sense buy it's actually
>> quite rare I think. People who work with objects big-time may have use for
>> it, but I don't think it's actually all that necessary. It's good to have
>> and good to know what it is and what it does, but on most days you'll be
>> fine without it.
>
> I think it is done to lower the overheads if for example you only need to
> use 1 function within the class. Instead of having the overhead of
> instantiating the class then calling the method (which I presume allocates
> memory for the methods and properties).
imho it would also be bad design / sort of misleading to create an
object you only use for calling a function on that does not need any
information from the object itself. With php5 you can also declare the
function static, so that when the code is maintained (maybe years later
by another programmer) it won't get modified to expect the object to be
properly initialized.
To say it an other way, making a static function and using :: will
communicate your intentions more clearly in your source code.
(i don't think the methods need extra memory for each extra object you
instantiate. And php only adds properties that are either declared or
actually assigned. But the object itself will probably be allocated, and
deallocated when it is no longer referenced, so i think you are right
about the overhead)
Greetings,
Henk Verhoeven,
www.phpPeanuts.org.
Navigation:
[Reply to this message]
|