Asked  7 Months ago    Answers:  5   Viewed   37 times

In PHP, what do you mean by function overloading and function overriding. and what is the difference between both of them? couldn't figure out what is the difference between them.



Overloading is defining functions that have similar signatures, yet have different parameters. Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that method.

In PHP, you can only overload methods using the magic method __call.

An example of overriding:


class Foo {
   function myFoo() {
      return "Foo";

class Bar extends Foo {
   function myFoo() {
      return "Bar";

$foo = new Foo;
$bar = new Bar;
echo($foo->myFoo()); //"Foo"
echo($bar->myFoo()); //"Bar"
Wednesday, March 31, 2021
answered 7 Months ago

There is no general difference between variables in PHP 4 and 5.

What you are probably referring to is the ´E_NOTICE` error reporting level. When that level is turned on, PHP will complain if a variable is used that hasn't been assigned yet. That level existed in PHP 4 already:

// Report all errors except E_NOTICE
// This is the default value set in php.ini

error_reporting(E_ALL ^ E_NOTICE);

echo $hello_world;  // Will output nothing, but also not output a notice


echo $hello_word;   // Will output "Notice: Undefined variable"

PHP's error reporting can be influenced through the "error_reporting" php.ini setting, or during runtime of the script using the error_reporting() function.

As for other differences, there's a load of them. Check out Gordon's link about Migrating from PHP 4 to 5.

Saturday, May 29, 2021
answered 5 Months ago

Overloaded methods can still be overridden, if that is what you ask.

Overloaded methods are like different families, even though they share the same name. The compiler statically chooses one family given the signature, and then at run time it is dispatched to the most specific method in the class hierarchy.

That is, method dispatching is performed in two steps:

  • The first one is done at compile time with the static information available, the compiler will emit a call for the signature that matches best your current method parameters among the list of overloaded methods in the declared type of the object the method is invoked upon.
  • The second step is performed at run time, given the method signature that should be called (previous step, remember?), the JVM will dispatch it to the most concrete overridden version in the actual type of receiver object.

If the method arguments types are not covariant at all, overloading is equivalent to having methods names mangled at compile time; because they are effectively different methods, the JVM won't never ever dispatch them interchangeably depending on the type of the receiver.

Tuesday, June 29, 2021
answered 4 Months ago

Declare your arguments as Optional Variants, then you can test to see if they're missing using IsMissing() or check their type using TypeName(), as shown in the following example:

Public Function Foo(Optional v As Variant) As Variant

    If IsMissing(v) Then
        Foo = "Missing argument"
    ElseIf TypeName(v) = "String" Then
        Foo = v & " plus one"
        Foo = v + 1
    End If

End Function

This can be called from a worksheet as =FOO(), =FOO(number), or =FOO("string").

Wednesday, June 30, 2021
answered 4 Months ago

You can return a different type, as long as it's compatible with the return type of the overridden method. Compatible means: it's a subclass, sub-interface, or implementation of the class or interface returned by the overridden method.

And that's logical. If a method returns an Animal, and your derived class returns a Cow, you're not breaking the contract of the superclass method, since a Cow is an Animal. If the derived class returns a Banana, that isn't correct anymore, since a Banana is not an Animal.

Thursday, July 1, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :