Am 04.08.2011 23:19, schrieb Chris Stockton:
I myself oppose WeakReference in java and I do not see my opinion
changing for PHP ... unfortunately I think WeakReference's are even
nastier in PHP because of it's error handling. You see it is very
common when weak references are used in java too follow one of two
paradigms, fall through with NPE's, or state/null checks. With PHP,
you may not "test" the validity of a weak reference simply by
accessing it, you will get a uncatchable fatal error. So in PHP your
only option when consumers are obtaining weak reference handles, is to
check their validity before use, whether that is a call into some
registry, or a null check of the reference itself, whatever.. it still
leads to checks to make sure that reference wasn't collected. This
added complexity in the WeakReference consumer makes me unable see any
purpose through the inconvenience and unpredictability they bring.
Though I have never used java, I believe Weak- and SoftReferences would
tremendously simplify certain, very specific applications.
* WeakReferences for implementing the observer pattern
aka. keeping a list of handlers for certain events.
* SoftReferences for caching
aka. keeping a list of objects to be returned or created by a factory
If used this way, there is no danger of null-fatal-errors, because the
event-trigger is aware of the volatile nature of its handlers and the
factory knows that it's cached objects might be discarded.
In fact, I'd say that Weak-/Soft-References are a better fit in PHP than
in Java, because the ->get() methods return type is non-specific (has to
be cast in java?)...which matches PHPs untyped approach.
I use ticks to do this in a CLI daemon I wrote, worked out pretty
well. Not saying a specialized function isn't needed but I think ticks
aren't to far from convenient to require such.
Could you explain ticks?...to me a tick is a nasty little creature that
bites and gives you infections;)
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php