> indication as to what the getters and setters are, resulting in an unclear
> API.
> Therefore, if the interface needs all of the getters/setters to be
> implemented in the classes that implement the interface, what use do the
> magic methods provide besides saving a few keystrokes?
You wouldn't define magic methods for interfaces. Think in terms of
templates. When you create an interface, you're defining a template
that will be applied to every model that implements it. The template
just gives you a list methods and the implementing class is required
to fill in the details of each method. That's why specifying magic
methods in an interface would create ambiguity; there's no way for
the implementing class to know which methods it's supposed to define.
If it helps, you can also think of an interface as a list of
requirements to be fulfilled by the implementing class.
You can add magic methods to models. You'll still end up with the
confusion of knowing whether or not an object supports different
properties. Matthew listed some benefits earlier in the thread.
Another benefit I was thinking of, you can add a field to a table in
the database and immediately start using it without having to update
your models and you can go back later to add business logic if you
need to.
Does that help?
-Ed
没有评论:
发表评论