2009年6月5日星期五

Re: [fw-mvc] get and set accessors

-- Hector Virgen <djvirgen@gmail.com> wrote
(on Thursday, 04 June 2009, 04:07 PM -0700):
> Looking at Zend_View_Interface, it has __set(), __isset() and __unset(), but
> not __get(). The abstract class, however, has __get().
>
> They do provide good documentation in the comments as to what they should be
> used for, so I can see now how interfaces can have magic methods. It's all
> about the comments :)

And for those who want to know the whole story...

The reason we did this is that we wanted to ensure that alternate view
engines would allow property access as a way to assign view variables;
this would ensure that all view implementations would act the same, and
could be swapped without needing to change the code.

The reason we didn't specify __get() in the interface was that in the
minimum PHP version supported at the time, arrays accessed via
overloading were de-referenced, which broke some use cases. As an
example, the following:

$view->foo[] = 42;
$view->foo['bar'] = 42;

were acting on copies of the foo property, and the new values would not
be assigned within the object.

We could get around it by using an ArrayObject container for all
variables -- except that, at the time, we also discovered that
ArrayObject in PHP 5.2.0 was broken, and the above still didn't work.
The workaround was simple: use __set() to assign new public properties,
and omit __get() (as public property access always works).

Now that array dereferencing is no longer an issue, and ArrayObject
works fine, we have a new path forward -- but we'll need to wait until
ZF 2.0.0 to change the interface.


> On Thu, Jun 4, 2009 at 3:47 PM, Ed Lazor <edlazor@internetarchitects.biz>
> wrote:
>
> Side note that might help clarify things here...
>
> The answer to your question is that you would know based on project
> documentation, coding style, or just simply because you're the only
> one doing the coding.
>
> If you're asking the question as a way of making a point, that you
> think magic methods shouldn't be used... All I can really do is share
> my personal opinion. It seems like I should be using the same
> standards that are applied to ZF for all of my work, regardless of
> whether the work is personal or professional. And that's mainly
> because I don't want to build bad habits during personal projects...
> It seems like I really shouldn't be relying on magic methods...
>
> -Ed
>
>
> On Thu, Jun 4, 2009 at 3:38 PM, Ed Lazor<edlazor@internetarchitects.biz>
> wrote:
> > On Thu, Jun 4, 2009 at 3:20 PM, Hector Virgen<djvirgen@gmail.com> wrote:
> >> I understand the benefits of magic methods, but how will I know if my
> object
> >> even has them (or which properties can be set) if it's not in the
> interface?
> >
> > You put the magic methods in a basic model class and then extend
> > everything from there.
> >
> > Did you understand what I said about why you wouldn't put magic
> > methods in the interface?
> >
> > -Ed
> >
>
>

--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: