Polymorphism

2011-11-24 20:22:12

Of course my first reaction to the simple question regarding Polymorphism was stunned silence.  I hadn’t thought about that word more than a couple times since college.  Hell, even then I probably wasn’t completely sure what it meant (solid use of that education, Joe).  However I knew it was important.

With some dumb luck, I was able to talk about.. Well.. Something.  Not totally sure if it made sense whatsoever but I was able to talk about what I thought it meant and how I saw it in practice.

During my personal follow up, my first inclination was to check the almighty Wikipedia and what they had for Polymorphism,

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method,field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run-time (this is called late binding or dynamic binding).

- source

Hmm.. Ok, I know I’ve seen that before.

Actually, it was something that had baffled the heck out of me from earlier learning about PHP.  I had learned in C how one could define two similarly named functions where one would take a different number of arguments than the other.  So depending on how many arguments are passed, the particular function would be used.

Well isnt possible with PHP due to the fact that it allows argument overloading.  However with the use of Type Hinting it is possible to achieve Method Polymorphism.

The example given at the Geek Files is pretty helpful in understanding a few principles all at once.

class BaseClass {
   public function myMethod() {
      echo "BaseClass method called";
   }
}
 
class DerivedClass extends BaseClass {
   public function myMethod() {
      echo "DerivedClass method called";
   }
}
 
function processClass(BaseClass $c) {
   $c->myMethod();
}
 
$c = new DerivedClass();
processClass($c);

You can follow the jump above or follow my crude interpretation:

The object $c is executed with processClass() and while the function is defined with an argument of BaseClass since $c is of DerivedClass, that public function myMethod() will be called and “DerivedClass method called” will be echoed.  I highly recommend checking out the link to Geek Files, it’s really well written and the examples are excellent.

Something mentioned a few times over was “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism.”

Within my CodeIgniter-based SiteApp, I have a utility_model that contains many of my most used functions as well as several key CRUD functions - so I tend to extend this model with all my custom made ones.  In doing so, I tend to ‘overwrite’ many of the utility_model functions with ones located inside the custom model.  Not all of the utility_model functions are repeated and are still accessible in its base form.  For example, I have my utility_model function to get all rows from a table:

public function get_all($table) {
    $this->db->order_by('id','asc');
    $query = $this->db->get($table);
    return $query->result();
}

And within my custom model:

public function get_all() {
    return parent::get_all('media');
}

Now, even if I call the function with $this->custom_model->get_all(), only the new custom function will call and get all rows from the media table.