(on Thursday, 04 June 2009, 01:27 PM +0200):
> Matthew Weier O'Phinney wrote:
> > > -- Matthew Ratzloff <matt@builtfromsource.com> wrote
> > > (on Wednesday, 03 June 2009, 11:16 AM -0700):
> > > > Using __get() and __set() for properties known at the time of
> > > > writing is a minor time saver for you, the programmer, and can
> > > > be a major time waster for those trying to debug or otherwise
> > > > understand your code. It's a bad practice.
> > >
> > > I disagree, and feel it's a documentation issue.
>
> I disagree, and think it depends on your development mantra.
>
> Two major issues come to my mind:
> 1) Unit tests
> 2) Code completion in IDE
>
> You cannot really test objects with magic methods, because PHP stops
> emitting hard errors when trying to access undefined methods or
> properties. Those errors would normally stop you from trying something
> inappropriate with the object. Omitting those errors might result in
> hours of debugging.
This of course depends on how you implement it. I typically will throw a
BadMethodCallException or InvalidArgumentException for methods I'm not
trapping or if the property is one I'm not handling. In the
implementations I've done, I only allow virtual getters/setters and
__get/__set access for protected methods defined in the class, and throw
an exception for any others.
> If the methods and properties are not enumerated in the code, but result
> from the correct execution of code, no code completion logic can work.
> Bad thing.
While I understand where you're coming from, this is also a matter of
what development environment you use. In Vim, I get autocompletion for
any property or method call I've previously used. Eclipse, on the other
hand, will give you no feedback (currently). I personally feel we need a
PHP docblock standard for enumerating method and property overloading,
and for IDEs to honor this in order to provide code assist.
> > > Doing so can often make a class forward-compatible with future
> > > versions by ensuring uniform accessors and properties tied to
> > > accessors from the start. You may not need them immediately, but
> > > you may in future iterations, and adding them later would be a BC
> > > break and/or require adding __set and __get anyways, but now with
> > > more complex logic.
>
> YAGNI! :)
Again, opinions will differ here. I *have* neede it. ;)
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
没有评论:
发表评论