Method Overloading in PHP5

Although with release of PHP5 we finaly got some long awaited OOP features, sometimes I really miss overloading capability which exists in languages like Java. I am talking about something like this:

class Overloading_Test
{
  public void hello()
  {
    System.out.println("Hello Anonymous");
  }

  public void hello(String name)
  {
    System.out.println("Hello " + name);
  }


  public void hello(String firstName, String lastName)
  {
    System.out.println("Hello " + firstName + " " + lastName);
  }
}

This way you can call either hello with no arguments at all, or with one or two arguments, and proper method would always be called. Unfortunately, if you try something like this in PHP, it would give you fatal error, because basically, methods cannot be redeclared, since support for overloading is not part of core language like in Java.

However, there is still a way to achieve this Java like overloading functionality by using “magic” methods that are described in PHP Manual. Although it is not clear from manual how could you achieve exact functionality like in Java, I played a little bit with __call function, and get interesting workaround.

<?php

class Overloading_Test
{
  function __call($method_name, $arguments)
  {
    //list of supported methods
    //only 'hello' for this test
    $accepted_methods = array("hello");

    //in case of unexistant method we trigger fatal error
    if(!in_array($method_name, $accepted_methods))
    {
      trigger_error("Method <strong>$method_name</strong> doesn't exist", E_USER_ERROR);
    }

    //we inspect number of arguments
    if(count($arguments) == 0)
    {
      $this->hello1();
    }
    elseif(count($arguments) == 1)
    {
      $this->hello2($arguments[0]);
    }
    elseif(count($arguments) == 2)
    {
      $this->hello3($arguments[0], $arguments[1]);
    }
    else
    {
      return false;
    }
  }

  function hello1()
  {
    echo "Hello Anonymous<br>";
  }

  function hello2($name)
  {
    echo "Hello $name<br>";
  }

  function hello3($first_name, $last_name)
  {
    echo "Hello $first_name, $last_name<br>";
  }
}


$ot = new Overloading_Test();
$ot->hello();
$ot->hello("John");
$ot->hello("John", "Smith");
//this one will produce fatal error
//$ot->test();
?>

If you run this code, you will get something like:

Hello Anonymous
Hello John
Hello John, Smith

So, what is going on here? Whenever we call some undeclared method (which is the case with ‘hello’ method here), magic method __call is called, and two arguments (method name and arguments) are passed to it. For this simple test, we only support overloading of ‘hello’ method, so in case you try any other, we trigger fatal error.

What’s going on further is, we simple check number of argumens passed (by counting $arguments array), and call proper method. For the sake of clarity, I only used simple overloading based on number of arguments, but you could also check for argument type (ie string, integer etc.) and call proper method.

So, as you see, method overloading in PHP5 is not as elegant as in Java, but you can still make it. For more information about ‘magic’ fucntions (there are quite a few for member overloading as well), please visit PHP Manual.

Comments

  1. January 19th, 2017 | 11:51 pm
  2. January 20th, 2017 | 12:26 am
  3. January 20th, 2017 | 12:51 am
  4. January 20th, 2017 | 1:16 am
  5. January 20th, 2017 | 1:59 am
  6. January 20th, 2017 | 3:17 am
  7. January 20th, 2017 | 5:08 am
  8. January 20th, 2017 | 5:42 am
  9. January 20th, 2017 | 8:58 am
  10. January 20th, 2017 | 9:15 am
  11. January 20th, 2017 | 10:49 am
  12. January 20th, 2017 | 12:07 pm
  13. January 20th, 2017 | 6:49 pm
  14. January 20th, 2017 | 7:25 pm
  15. January 20th, 2017 | 9:19 pm
  16. January 20th, 2017 | 11:32 pm
  17. January 21st, 2017 | 12:08 am
  18. January 21st, 2017 | 12:41 am
  19. January 21st, 2017 | 12:59 am
  20. January 21st, 2017 | 2:01 am
  21. January 21st, 2017 | 2:10 am
  22. January 21st, 2017 | 6:51 am
  23. January 21st, 2017 | 11:22 am
  24. January 21st, 2017 | 12:42 pm
  25. January 21st, 2017 | 2:03 pm
  26. January 21st, 2017 | 2:57 pm
  27. January 21st, 2017 | 4:17 pm
  28. January 21st, 2017 | 4:36 pm
  29. January 22nd, 2017 | 1:07 pm
  30. January 22nd, 2017 | 2:30 pm

    I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get bought an nervousness over that you
    wish be delivering the following. unwell unquestionably come
    further formerly again as exactly the same nearly a lot often inside case
    you shield this increase.

  31. January 22nd, 2017 | 3:13 pm
  32. January 22nd, 2017 | 3:42 pm
  33. January 22nd, 2017 | 4:21 pm
  34. January 22nd, 2017 | 5:37 pm
  35. January 22nd, 2017 | 6:36 pm
  36. January 22nd, 2017 | 7:05 pm
  37. January 22nd, 2017 | 7:43 pm
  38. January 22nd, 2017 | 8:51 pm
  39. January 23rd, 2017 | 12:19 am
  40. January 23rd, 2017 | 2:59 am
  41. January 23rd, 2017 | 3:28 am
  42. January 23rd, 2017 | 8:00 am

    Good day! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading your posts.
    Can you recommend any other blogs/websites/forums that cover the same topics?
    Thanks a lot!

Leave a reply