Function Overloading in PHP

April 19th, 2005

Update: I have written about a more Java-like, argument based overloading solution here.

I’ve been trying to get a set of good basic functions included within my PHP framework and ran across a nice little trick to allow function (without a class) overloading, the code should speak for itself.

This will be the default file.

if(!function_exists("overloadableFunc") ) {
  function overloadableFunc() {
    echo "foo";
  }
}

and this will be in the overloading file (needs to be included before the default.

function overoadableFunc() {
  echo "foobar";
}


4 Comments to “Function Overloading in PHP”


  1. Marl Atkins said:

    Hmm, what if I want to use the overloaded call in the overloading class?
    I can do this is C# and Java:

    In this case I’m really doing both an overload AND an override.
    That is, the second class uses the name of a function that is also included in the class that it extends, but the number of arguments are not the same.

    I fear this is going to be a nightmare in php.
    I’ve seen some code that uses args[] to kind of simulate it.

    Any ideas?
    [code]
    class Overloaded
    {
    function OverLoadMe($sVal1, $sVal2)
    {
    return $sVal1 . $sVal2
    }
    }

    class OverLoader extends Overloaded
    {
    $sVal1 = “I got this internally, maybe from a database”;
    function OverLoadMe($sVal2)
    {
    return this->OverLoadMe($sVal1, $sVal2);
    }
    }
    [/code]

    Florida Web Design, Inc. of Orlando


  2. Kir said:

    http://digg.com/celebrity/Lisa_Ann_2


  3. Cutreda said:

    If you have a little free time, read this post:,


  4. Sindrug said:

    Do you want to see good pics?,

Leave a Reply