first_page

Taking OOP Features in PHP 5 Seriously

I have been suffering as a PHP programmer because I have not been taking the OOP features in PHP 5 seriously. This means that have been completely ignoring them so I can get ‘real work’ done—where ‘real work’ refers to the kind of work BASIC programmers did over a decade ago.

I’m working on this PHP project with 12 entities requiring 12 data forms and 12 CRUD operations. The refrain to this song is filled with the wish that when a new entity comes along it can just be ‘plugged’ in as an object instead of as a series of discontinuous blocks of code scattered in a half dozen places. This sounds like a job for PHP5. These are the tools:

  • The static keyword. This allows me to place code that would otherwise end up in switch structure in some general-purpose function. Yes, it’s better to have 12 different static methods encapsulated in 12 different classes (in 12 different files) than one big-ass general-purpose function with 12 different case blocks.
  • The Scope Resolution Operator (::). This is the syntax used to call static methods. I’m thinking this comes from C++. The self:: or parent:: syntax comes in handy.
  • Object interfaces (the implements keyword). When I really need a big-ass general-purpose function, it can take an object implementing an interface as an argument and really up the elegance. The instanceof operator should be handy here.

rasx()