Tuesday, November 16, 2010

Simple recipe for trouble (PHP)

1. In a base class, make a method which returns the date in the given format, with some specific text format as default, like this:

        class MyHumbleBaseClass {
   function getDateParam($format="Y-m-d h:m:s") {
              $timestamp = .... 
              if ($format==null) return $timestamp;
              else return $this->formattedDate($format);

2. Override it in some derived class like that:
class MyHumbleDerivedClass {
           function getDateParam() {
              return MyHumbleBaseClass::getDateParam();

3. To add some extra twist, forget that the default format is not null, and do things like that:

if ($derivedObject->getDateParam()<$timestamp) {
    // think that it should work
4. Enjoy.

PS In fact, simple overriding will probably not be enough; if you supply the parameters, you will end up calling the parent function. But if you use the base method as a strategy from within your other class, thus shielding the direct access to the base class completely, then the success is practically guaranteed.