daveross/phpoverloading

This package is abandoned and no longer maintained. No replacement package was suggested.

Method overloading for PHP objects

v1.0 2016-07-03 18:13 UTC

This package is not auto-updated.

Last update: 2023-06-30 16:06:36 UTC


README

PHP trait providing a form of method overloading in PHP. Useful when porting code from languages which support method overloading, and that's about the only time you should seriously consider using this library.

Seriously, if you're thinking of using it for new PHP code, ask yourself why. There's no reason to add this kind of overhead, not to mention the cognitive load of weird method names, just to overload methods. PHP has gone this long without native method overloading. It can go longer without it.

Just stop. Think of the children.

License

MIT

See why I contribute to open source software.

Buy me a coffee

Contributing

Pull requests are welcome. Unit tests are encouraged but not required.

Installation

Using composer

Put the require statement for phpoverloading in your composer.json file and run composer install or php composer.phar install:

{
    "require": {
        "daveross/phpoverloading": "~1.0"
    }
}

Manually

Include the file in the src directory, or individual files as needed:

<?php
include 'path/to/phpoverloading/src/OverloadedMethods.php';

Using

Begin by applying the trait to your class by having use \DaveRoss\PHPOverloading\OverloadedMethods; inside the class body.

Supported parameter types

All of PHP's native types are recognized:

  • boolean
  • integer
  • float
  • string
  • array
  • object

In addition, the trait recognizes class names and class hierarchies.

A note on method naming

PHP doesn't allow functions or methods to have the same name. That's probably the biggest obstacle to actual method overloading. So you'll need to append the parameter types to each function's name.

For example, if you had two methods named example, one taking a string and one taking an array, you'd name them example_string and example_array, but when you called then you'd just reference $foo->example() and pass a string or array and magic happens.

If a method takes more than one parameter, their types are concatenated together in the function name, separated by underscores as in example_string_integer_float. Overloaded methods can take different numbers of parameters.

Constructors are like any other functions. You can have __construct_string and __construct_object if you want. But there's also a _default suffix (i.e. __construct_default) for a default constructor, called when there aren't any parameters passed to the constructor.

The class hierarchy

Say you have these classes:

class A {}
class B extends A{};

class Foo {
    function __construct_a() {
        ...
    }
}

If you call Foo's constructor and pass an instance of class B, the trait won't find a matching __construct_b function, so it'll follow the object hierarchy and call __construct_a instead, passing the instance of B.

If there wasn't a __construct_a method, it would look for __construct_object as a fallback.

Constructor overloading

class Example {

    use \DaveRoss\PHPOverloading\OverloadedMethods;

    public $string, $integer;

    public function __construct_string($val) {
        $this->string = $val;
    }

    public function __construct_integer($val) {
        $this->integer = $val;
    }

}

$strExample = new Example("hello world");
$intExample = new Example(5);

Method overloading

class Example {

    use \DaveRoss\PHPOverloading\OverloadedMethods;

    public $string, $integer;

    public function example_string($val) {
        echo 'the string is ' . $val;
    }

    public function example_integer($val) {
        echo 'the integer is ' . $val;
    }

}

$x = new Example();
$x->example("hello world"); // echoes "the string is hello world"
$x->example(5); // echoes "the integer is 5"