Instead of thinking of overloaded functions as many functions with the same name that have to be distinguished by their arguments, think of function overloading as providing multiple definitions for one function, with the correct implementation being chosen based on the arguments.
Scripting languages generally don't do function overloading. Instead, you have to manually mimic function overloading by switching on the arguments' types at run-time, viz.
function isEmpty($object)
{
if (is_scalar($object)) return
strlen($object) == 0;
if (is_array ($object)) return count
($object) == 0;
if
(is_null ($object)) return false;
if (is_object($object)) return false;
return !!$object;
}
In a language that does have function overloading, the definition of
isEmpty could be split into multiple functions, each with the same
name but accepting different parameter types.
That makes it complicated to refer to those functions by name since name alone does not uniquely identify one of them. But if function overloading were thought of as syntactic sugar for the above case of a function switching on the type of its arguments, then there would be no difficulty since there would be only one function still. Or, alternatively, you could think of the function name implicitly referencing all of the overloaded functions, with the correct one being selected at call time.
Hmm. But what happens when we want to add a new class which can also have isEmpty applied to it? Do we have to go back and edit this same function and add an extra
if (is_my_class($object))
clause? If I have a hundred classes, all of which can overload an operator like + does that mean the + function will have a hundred armed case statement?
— PhilJones