33 views

CHAPTER 14 - PHP Design Tip #3: Do Not Over Design!

With PHP 5’s new OO features, it is easier to make clean object-oriented
designs. PHP has a vast amount of built-in functions and functions provided
by various extensions, most of which are procedural (calling functions rather
than working with objects).

OO Wrappers for Built-In Functions
To make interfaces “cleaner,” it
may be tempting to wrap a class layer around built-in functions. Unless these
wrappers provide real value, they just add bloat and complexity. “Real value”
could be providing a unified API to different extensions (similar to, for exam-
ple, PEAR DB), or it could be adding new, higher-level functionality (similar to
PEAR Net_Socket).

Generalize Carefully
Generalization is expensive (saying it is
cheap). Know why you make something more general or abstract, and think
about what you expect to gain from doing it. If you add abstractions without
knowing exactly why you need them, chances are you are making another
abstraction that you need further down the road.

Do Not Pretend PHP Is Java!
PHP and languages such as Java or
C++ are vastly different. One thing is that PHP is compiled at runtime, but
PHP has a huge amount of low-level, built-in functionality that Java provides
through higher-level packages. Even though PHP 5 has a vastly improved
object model, object instantiation in Java is several times faster than in PHP.
Java has String objects, while PHP has a string type. Java has a Vector class, and PHP has arrays. Writing a Vector class for PHP could be interesting
as an exercise, but for production use, it is just silly because PHP has built-in
functionality for doing the same thing much faster.
PHP applications need to be designed as PHP applications that accommo-
date PHP’s different strengths and weaknesses.

Post a Comment

You must be logged in to post a comment.