The Advancing PHP Developer Part 2: Object Orientation

PHP Web development May 25th, 2008 by Eran Galperin

Money(I know I'm due the last part on Models in the Zend Framework. I'm getting there, it's a pretty long rant so it'll be here eventually)

Continuing with (PHP) development best good practices:

2. Object Orientation

Object Oriented programming is a software architecture methodology that became mainstream in the mid 1990's. It means different things to different people, and there is much debate whether it's any improvement over more traditional procedural programming. I won't get into that debate, but I'll quickly recap why I consider OO very important for a serious developer:

  • It provides structure and modularity to your code. While you can duplicate that in procedural programming, OO gives you language tools to deal with it (classes).
  • Code re-use. Modern OO languages (PHP included) give you inheritance and polymorphism in addition to containing code segments in functions (methods in OO lingo).
  • Encapsulation (or code visibilty) is another OO methodology trait that gives you more tools when designing interfaces for your code. It relates to the idea of code re-use by hiding implementation details from the outside, therefor promoting better interfaces to be created. PHP5 implements code-visibility features such as the 'public', 'private' and 'protected' keywords.
  • Static types. Static methods and members of a class can replace the use of globals and defines almost completely, reducing global namespace pollution (read a little about why globals are 'evil').

In addition, almost all professional code packages are written completely in OO programming. So if you have any intention of using some very powerful code libraries, you better get along with OOP ;-).

Extra reference material:
PHP manual on OO (read also the part on PHP5 specific features)
Wikipedia - Object Oriented Programming
Introduction to object oriented programming in PHP
OO versus Procedural Programming in PHP

Previously: Coding standards, Next up: Refactoring

If you liked this article you should follow me on Twitter and/or share below: