Function Overloading and PHP

I’m proud of writing elegant code that can be easily read and intuitively understood. I use whitespace to increase my code’s readability, and I utilize simple, specialized programming language constructs to make my code more concise. For example, I’ll use PHP’s foreach construct, instead of a traditional for loop, to eliminate unnecessary indexers. This construct can also be found in Java, C#, Perl, as well as other programming languages. I also frequently use a PHP idiom, which I have never used in other languages, that allows for the convenient addition of elements into an array.

Because PHP features nonessential elements such as the array append operator, I am irked by PHP’s lack of a more useful feature: function overloading. The popular approach of appending a formal argument’s type to a function name is bothersome and inconvenient. The scheme begins to deteriorate source code aesthetics when the functions that require overloading accept more than one argument. Additionally, calling functionName($objectOfTypeA) and functionName($objectOfTypeB) is clearly more elegant than having to call functionNameTYPEA($objectOfTypeA) and functionNameTYPEB($objectOfTypeB). Function overloading would allow people to use a consistent interface, while allowing the implementation of a function to vary depending on the number and types of supplied arguments. Surely, there must be a way that type hints can be exploited to implement this much needed feature.

23 Responses to “Function Overloading and PHP”

Newer Comments »
  1. Anonymous Says:

    Wow, grats, you’re good at stating the obvious…
    This article, for some reason, reminds me of the “I like turtles” kid…

  2. Anonymous Says:

    I find php’s handling of ‘overloading’ (in the sense that you use it) is very convenient. You can just use the if statement and determining the type of parameters to replicate the behaviour. However, if you want the integers and say only one string value also (eg ‘infinity’ or ‘any’) or to overload with a set (array) but keep all the functionality in one function, then php allows you to do this very easily. It is not a bug of php, it is a design choice. However, for safety critical software where you want very rigorous design and more easy bug checking, there are other languages available.

  3. blackBoar Says:

    Just found your website after a query on the same subject, and I’m beginning to accept that the only way of emulating such thing is:
    @brief does something
    function doSomething($param){
    $type = gettype($param);
    // do something according to type

    //by default, the original doSomething()

  4. xx Says:

    really bad

  5. You fail Says:

    This article is an amazing example of how to say absolutely nothing useful in a surprisingly large amount of space. I hope your “elegant” programs are more useful than this piece of junk.

  6. bearman Says:

    Mr. “You fail” — we are soo grateful for your contribution to humanity.
    Nonetheless, I could not help but notice that your own comment does not escape your own destructive criticism: it is useful for nothing.

  7. seems Says:

    Actually Mr bearman, you re very wrong. trying to be smart in your response you have only appeared as shallow as one can be. “You fail” very clearly, accurately and precisely defines the content of this post. It is extremely useful to read his post before any other on this page because it contributes in the decrease of waste of time to humanity. Your post Mr bearman is even more obnoxious than the piece of junk of an article itself.

  8. mmm Says:


  9. julesmanson Says:

    I agree with steve doria of this site however I would take my own magic overload function one step further and have it automatically load as soon as the php/mysql software is up and running on the server instead of invoked from a class object defined on some page and loaded with __autoload. Now that’s real magic for ya. I am sure there is a way via config.php or .htacccess but I just don’t know how to do that yet. Perhaps someone can point me in the right direction. Thanks.

  10. Drizd Says:

    I agree, not a good article, but it does touch on the important – PHP’s inability to stand with other “proper” object oriented languages have made me dump it in favour of other technologies for numerous projects.

  11. gregx Says:

    it is funny, i just started to look and learn php about 1/2 years ago and was googling about the same issue and got here. I wouldn’t say anything bad about the article but it seems to me some comments are made by people who are never used any other language besides php. This is disgrace! Probably that’s why all those php sites lack security so often because they are made by narrowminded lazy programmers!

    PS. started 25 years ago in asm

  12. Edgarin Says:

    Although function/method overloading can be “emulated” in the case we want different behavior for different parameter data types or number of parameters; don’t forget there are many other cases where we need method overloading that can’t be emulated, for example:

    (This code doesn’t compile, of course)
    class AClass{
    var $member;
    static function myMethod() { echo “Static”; }
    function myMethod() { echo $this->member; }
    AClass::myMethod(); //Should display “Static”;
    $obj = new AClass();
    $obj->myMethod(); //Should display contents of $member

    Unfortunately, in this example, there is no other way than renaming one (or both) methods.

  13. maximus Says:

    Some of the replies to this article are another example displaying that PHP harvests the scum of the programming world. A community full of know-it-all ignorants, that never seize the chance they’re given to shut up and learn something new. For all the convenience the language offers, other aspiring programmers, who truly want to get better at their craft, have to put up with this nonsense.

    I’m sorry I had to get it out of my chest.

    Now, offering something constructive that hopefully highlights a bit more why the current language’s overloading facility lacks.

    Sound software engineering principles will tell you “code to an interface, not an implementation”. Therefore if you want to create a generic Validator signature, you could just do something like this.

    Interface Validator {
    function setValues($values);
    function isValid($values);
    function isValid();

    This specifies that the validator object can optionally receive data in its isValid method, but it’s not mandatory (maybe you’ve already set data previously with setValues()).

    With this signature, you’re not forced to separately call setValid() and isValid(), but you’re not forced either to have an isValid method that absolutely requires to take in a parameter. Unfortunately due to PHP’s lack of true method overloading, you can’t define such an interface.

    I can see people coming a mile away with stuff like

    function isValid($array = null);

    Wrong! Interface, remember? You should specify if the function takes a parameter or not.

    The hacking spirit, rampant in PHP has had people used to McGyverish code where, instead of just directly working toward its primary goal, a function has to check the state of its parameters.

    Lets say your interface define the signature as:

    function isValid($array);

    and that you implement as:

    function isValid($array=null)

    Even if you do this, you still won’t be able to call your method like


    because the Interface requires that isValid takes a parameter. You will be forced to do:


    Tell me this isn’t ugly.

  14. Tanvir Says:

    I have shown a good example in my blog ( ).
    It’s clearly described there , that how overloading should implement.


  15. Marco Schuster Says:

    You can do this with , even if it’s not proper overloading.


  16. Marco Schuster Says:

    Link again:

  17. this is a good article Says:

    i don’t know why there is any criticism. it’s a valid point, and well-defended.

  18. Ravinder Kumar Says:

    I still can’t understand what is function overloading so please say something more about it.

  19. FSGale Says:

    @Ravinder Kumar

    Function overloading has been discussed above, but here is a clear example:

    See how two functions are defined with the same name? This is true function overloading by having two or more functions with the same name, but have different input parameters (and in some languages where you can actually specify the expected return value(s), this too can vary from method to method). This is not actually possible in PHP natively, and must be handled in different ways. One example of how this is possible is the following:

    elseif (1 === $argc && is_integer($argv[0]))
    return $this->secondFunc($argv[0]);
    /* if you need more conditionals based on function input, add here */
    return false;
    private function firstFunc($myVar)
    /* do something */
    private function secondFunc($myVar)
    /* do something */

    Now there are other methods of handling this circumstance with PHP (examples have been provided above), but this should at least give you enough information to see how it can be done. If you prefer another method, then by all means use it if you feel comfortable with it. I honestly have not done any sort of performance testing to see which method may be better overall, so as far as I’m concerned they are most likely all equal (relatively) or at least close enough that it most likely doesn’t matter which one you do use. Obviously the conditionals in myFunc of the above example may need to be entirely changed to adjust for your own overloading conditions.

  20. FSGale Says:

    ooops… I guess php tags aren’t acceptable for this. The first example should look like:

    class myClass
    public function myFunc();
    public function myFunc($myVar);

    The second example only partially was displayed and should have been (I adjusted the conditionals for a more readable example):

    class myClass
    public function myFunc()
    $argc = func_num_args();
    $argv = func_get_args();

    if (1 === $argc)
    if (is_string($argv[0]))
    return $this->firstFunc($argv[0]);
    elseif (is_integer($argv[0]))
    return $this->secondFunc($argv[0]);
    return false;
    private function firstFunc($myStr)
    /* do something */
    private function secondFunc($myInt)
    /* do something */

Newer Comments »

Leave a Reply