Harrie's blog
Monday, 5 May 2008Accessing object properties by referenceReacties
Geeft reacties weer als
(Lineair | Samengevoegd)
'This will simply cause an error then myVar isn't an object' confused me for a moment, guess it's a typo for 'when'?
Thanks for the article
Ah, yes that's a typo. Fixed it now. Thanks for letting me know!
Don't you think that protected member require special cases and treats? There's nothing unusual here IMO, except the fact that you really do not care about protected variable.
BTW, please use English in comment's form
Hi Piotr, I'm not 100% sure if I get your question right. In this example there is no difference between protected membervars and public membervars - both will have the same effect. In other words: if you make the membervar (and/or the printMyVar method) public there won't be a difference - output remains the same.
Just to make sure: the 2 last code examples are ment to be in a method in the same class, for example in the runMe method. Now I look at it I see it can cause some confusion because they're just lines of code without any context. I hope this answered your question, if not please let me know. On and for the English form, click 'English' on top of the page and let the magic happen
Yeah, protected or public type won't change much here
Clearly this is unwanted behaviour of the Zend Engine, but not something you would easily encounter, unless you care squat about good practice.
Yet another reason NOT to expose an objects inner structure, and use accessor methods.
I just encountered this behavior this afternoon, and was stumped by it until I read your article. Very insightful! And while I'm tempted to forge ahead and leave this crazy code in place, ultimately it is so bizarre that I think other developers would have a hard time finding it if an issue ever arose.
The application in question uses a large, multi-level, hierarchical hash table to keep track of many resources. I'm delegating branches of the table down in the hierarchy, passing them by reference so that changes made are automatically reflected in the main. Using previous code as a template, I had added a level and unknowingly specified a branch that was not yet available as part of a __construct call, passing it in by reference. PHP went ahead and created the relationship. Later, when the data elements were populated, the relationship worked exactly as expected. I only caught the behavior by watching closely in my IDE, and wondered why I didn't see the attribute being set at instantiation. PHP must have made a StdClass in the background, as you said. It was easy, but it doesn't feel right. Thanks for the article!
Amazing that my blog post apparently is still useful even three years after writing it! Yes, it's a tricky problem and I'm glad my article helped you to track it down. Did you change anything in your methodology to prevent the problem from occurring again? (This might be a great advice for other developers!)
Thank you for your reply! |
Blog











