Function Overloading in PHP5 and PHP4

This is a solution to the problem of argument type based overloading in PHP. As far as I know this should work in PHP4 and PHP5.


class Viewer {
  function display($v) {
    $class = get_class($v);
    $function = "display_$class";
    if(method_exists($this, $function) ){
      $this->$function($v);
    } else {
      $this->display_default($v);
    }
  }
}

This class can then be extended and used like this:


class PDOViewer extends Viewer {
  function display_PDO($item) {
    echo "This is a PDO item";
  }
}
$viewer = new PDOViewer;
$viewer->display(new PDO);

2,265 thoughts on “Function Overloading in PHP5 and PHP4

  1. This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your great post. Also, I ave shared your site in my social networks!

  2. Thanks a lot for the helpful posting. It is also my opinion that mesothelioma cancer has an very long latency period of time, which means that the signs of the disease may not emerge until 30 to 50 years after the first exposure to asbestos. Pleural mesothelioma, which is the most common kind and is affecting the area across the lungs, may cause shortness of breath, chest muscles pains, and a persistent cough, which may produce coughing up blood vessels.

Leave a Reply

Your email address will not be published. Required fields are marked *