Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Overloading

Overloading provides means to dynamically create properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.

Property overloading is based on the magic methods __get, __set, __isset, __unset. Method overloading is based on the magic methods __call, __callStatic. There is no way to check if a overloaded method exist or not, unlike with properties.

Overloading does not apply to class constants.

Operator overloading is not supported. For example, it is not possible to define a - minus operator between two objects. This still exists for +, which applies to integers, floats and arrays. It may also be supported by specific PHP extensions.

<?php

    class Elephpant {
        function trumpet() {
            echo __METHOD__;
        }

        function __call($name, $args) {
            echo self::class. '::'. $name;
        }
    }

    $elephpant = new Elephpant;
    $elephpant->trumpet(); // Elephpant::trumpet
    $elephpant->stampede(); // Elephpant::stampede

    // existence check
    var_dump(method_exists(Elephpant::class, 'trumpet'));    // true
    var_dump(method_exists(Elephpant::class, 'stampede'));    // false
    var_dump(method_exists(Elephpant::class, '__call')); // true

?>

Documentation

See Also