HomePHP OOPSWhat is method overriding and method overloading in php

What is method overriding and method overloading in php

method overriding and method overloading

1. Method Overriding in PHP

Definition:
When a child class defines a method with the same name, parameters, and return type as a method in its parent class, the child’s version replaces (or overrides) the parent’s version when called from an object of the child class.

Key points:

  • Happens in inheritance (parent → child).
  • Method name and parameters must be the same.
  • Usually used to change or extend the functionality of the inherited method.
  • Access modifiers cannot be more restrictive than the parent’s method.
  • In PHP, you can still call the parent’s version using parent::methodName().

Example:

<?php
class ParentClass {
    public function sayHello() {
        echo "Hello from ParentClass\n";
    }
}

class ChildClass extends ParentClass {
    // Overriding
    public function sayHello() {
        echo "Hello from ChildClass\n";
    }
}

$obj = new ChildClass();
$obj->sayHello(); // Output: Hello from ChildClass
?>

2. Method Overloading in PHP

Definition:
In PHP, method overloading doesn’t work like Java or C++ (where you define multiple methods with the same name but different parameters).
Instead, in PHP, overloading means dynamically creating methods or properties using magic methods.

The magic methods for method overloading are:

  • __call($name, $arguments) → For inaccessible/non-existing instance methods.
  • __callStatic($name, $arguments) → For inaccessible/non-existing static methods.

Key points:

  • PHP does not support compile-time overloading (same method name with different signatures).
  • All arguments are passed as an array to these magic methods.
  • It’s useful for creating dynamic behavior in classes.

Example:

<?php
class MagicClass {
    public function __call($name, $arguments) {
        echo "Calling method '$name' with arguments: " . implode(', ', $arguments) . "\n";
    }

    public static function __callStatic($name, $arguments) {
        echo "Calling static method '$name' with arguments: " . implode(', ', $arguments) . "\n";
    }
}

$obj = new MagicClass();
$obj->doSomething("param1", "param2");
// Output: Calling method 'doSomething' with arguments: param1, param2

MagicClass::staticAction("paramA");
// Output: Calling static method 'staticAction' with arguments: paramA
?>

Quick Difference Table

FeatureMethod OverridingMethod Overloading
PurposeReplace inherited method behaviorHandle calls to non-existent methods
Requires inheritance?✅ Yes❌ No
Name & ParametersMust match exactly with parentCan be anything (magic methods handle it)
Compile-time or Run-timeRun-timeRun-time
PHP Magic Methods❌ Not needed✅ Uses __call / __callStatic

Alright — let’s make this crystal clear about the difference between:

  • Inaccessible/non-existing instance methods → handled by __call()
  • Inaccessible/non-existing static methods → handled by __callStatic()

1. Inaccessible/Non-existing Instance Methods → __call()

Meaning:
When you call an object method that either:

  • Does not exist in the class, or
  • Exists but is not accessible (e.g., private/protected from outside),

PHP will trigger the __call($name, $arguments) magic method.

Example:

<?php
class Example {
    public function __call($name, $arguments) {
        echo "Instance method '$name' called with: " . implode(', ', $arguments) . "\n";
    }
}

$obj = new Example();
$obj->sayHello("John", 25); 
// Output: Instance method 'sayHello' called with: John, 25
?>

Here:

  • sayHello() does not exist, so PHP redirects the call to __call().

2. Inaccessible/Non-existing Static Methods → __callStatic()

Meaning:
When you call a static method on a class that either:

  • Does not exist, or
  • Exists but is not accessible (private/protected from outside),

PHP will trigger the __callStatic($name, $arguments) magic method.

Example:

<?php
class Example {
    public static function __callStatic($name, $arguments) {
        echo "Static method '$name' called with: " . implode(', ', $arguments) . "\n";
    }
}

Example::sayHello("Mike", 30);
// Output: Static method 'sayHello' called with: Mike, 30
?>

Here:

  • sayHello() does not exist in static form, so PHP redirects the call to __callStatic().

Main Difference Table

Aspect__call()__callStatic()
Works forInstance methods (object calls)Static methods (class calls)
Triggered whenMethod doesn’t exist or is inaccessible in object contextMethod doesn’t exist or is inaccessible in static context
Signature__call($name, $arguments)__callStatic($name, $arguments)
Example usage$obj->methodName()ClassName::methodName()

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *