Stut schreef:
Jochem Maas wrote:
Stut schreef:
Nathan Nobbe wrote:
On Jan 29, 2008 7:27 PM, Stut <stuttle@xxxxxxxxx> wrote:
Personally I'd use a static method in this instance.
thats what i recommended.
If you need to create
an instance of the class you can do so in the static method and
that way it
will get destroyed when the function is done. Otherwise the object
scope is
far larger than it needs to be, which IMHO is an unnecessary waste of
resources and certainly less aesthetic.
lost you on this part ..
whether you create an instance in client code by calling new or
encapsulate the call
to new in a simple factory method there will still be only one
instance of the class,
and it will still be in scope once the method is finished executing,
because all it does
is return an instance of the class its a member of.
maybe you mean something other than what i posted earlier when you say
static method?
You posted a singleton pattern.
huh? the OPs getInstance() method returns a new object on each call,
hardly
a singleton is it?
Quite right too. Didn't read it properly.
That means that from the moment you call
the static method until the end of the script that object exists.
That's probably fine for web-based scripts that don't run for long,
but I live in a world where classes often get used in unexpected ways
so I tend to write code that's efficient without relying on the
environment it's running in to clean it up.
are you saying that the OPs getInstance() method causes each new instance
to hang around inside memory because php doesn't know that it's no
longer referenced,
even when it's used like so:
Test::getInstance()->doSomething();
and that your alternative does allow php to clean up the memory?
I could be wrong, I don't know the internals of PHP well enough to be
definitive, but I'd rather err on the side of caution than write leaky
code.
the way I understand garbage collection as it is right now is that pretty much
nothing is cleaned up until the end of the request but that php should be able
to see that the ref count is zero in both cases either way.
IIUC the yet to be released garbage collection improvements will potentially find/destroy
unused zvals sooner (as well as being better in sorting out defunct circular references etc)
but that the garbage collection itself uses a certain ammount of cpu cycles and in short
running scripts (e.g. most of what we write for the web) it's likely to be better to
let php just destroy memory at the end of the request.
that said your more cautious approach cannot hurt :-)
PS - my apologies if the memory related terminology I've used is somewhat bogus - please
put it down to my lack of proper understanding :-/
-Stut
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php