Too Much Java? 太像 Java 了?

A common criticism of use of interfaces in PHP is that it makes your code too much like "Java". What people mean is that it makes the code very verbose. You must define an interface and an implementation, which leads to a few extra key-strokes.

有人会说使用接口让 PHP 代码看上去太像 Java 了——即代码太罗嗦了——你必须定义接口然后实现它,要多按好多下键盘。

For small, simple applications, this criticism is probably valid. Interfaces are often unnecessary in these applications, and it is "OK" to just couple yourself to an implementation you know won't change. There is no need to use interfaces if you are certain your implementation will not change. Architecture astronauts will tell you that you can "never be certain". But, let's face it, sometimes you are.


Interfaces are very helpful in large applications, and the extra key-strokes pale in comparison to the flexibility and testability you will gain. The ability to quickly swap implementations of a contract will "wow" your manager, and allow you to write code that easily adapts to change.


So, in conclusion, keep in mind that this book presents a very "pure" architecture. If you need to scale it back for a small application, don't feel guilty. Remember, we're all trying to "code happy". If you're not enjoying what you're doing or you are programming out of guilt. step back and re-evaluate.

总而言之, 记住本书提倡“简单”架构。如果你在写小程序的时候无法遵守接口原则,别觉得不好意思。 要记住我们写代码是要快乐的写。如果你不喜欢写接口,那就先简单的写代码吧。日后再精进即可。

results matching ""

    No results matching ""