Download the PHP package daveross/phpoverloading without Composer
On this page you can find all versions of the php package daveross/phpoverloading. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download daveross/phpoverloading
More information about daveross/phpoverloading
Files in daveross/phpoverloading
Package phpoverloading
Short Description Method overloading for PHP objects
License MIT
Informations about the package phpoverloading
PHP Overloading
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
See why I contribute to open source software.
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
:
Manually
Include the file in the src
directory, or individual files as needed:
Using
Begin by applying the trait to your class by having 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 , one taking a string and one taking an array, you'd name them and , but when you called then you'd just reference 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 . Overloaded methods can take different numbers of parameters.
Constructors are like any other functions. You can have and if you want. But there's also a suffix (i.e. ) for a default constructor, called when there aren't any parameters passed to the constructor.
The class hierarchy
Say you have these classes:
If you call Foo's constructor and pass an instance of class B, the trait won't find a matching function, so it'll follow the object hierarchy and call instead, passing the instance of B.
If there wasn't a method, it would look for as a fallback.