-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Tim Starling wrote:
I wouldn't recommend leaving the var out when there is a need to initialise it to something simple, and you would need to add an isset(): [snip] The second one is slower.
Right. A little murkier would be something like
var $foo;
Which declares the variable and is a convenient point for documentation. I've always thought that adding this little marker allowed the Zend Engine to optimize access to the variable, much like a C struct. If I'm reading you correctly, apparently not! (It's really hard to test for micro-optimizations like these, and I can't read C well enough to make determinations on my own).
And, of course, conditional includes, although annoying to bytecode caches, let you bypass the overhead of loading all the opcodes altogether. There's a balance to be had there, and I'm curious to know what works and what doesn't.
But please don't forget my main point: an object is a hashtable, you can add and remove variables. Don't be such a stickler for self-documentation and the way things are "meant" to be done that you tie yourself in knots trying to avoid dynamic variable creation. You can always document with a comment instead.
There's an interesting question: why objects over arrays? (There's a simple answer to that, and it is that most of the time, you're dealing with an object, not an array, and shouldn't have to allocate another hash table for some custom properties.)
Also, you might be interested in the "Class Properties in Interfaces?" discussion that was recently floating around on php.internals, that proposed that unsetting a variable from an interface should cause a fatal error. :-P (You're probably not; it's a really long discussion).