Download the PHP package tightenco/overload without Composer
On this page you can find all versions of the php package tightenco/overload. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tightenco/overload
More information about tightenco/overload
Files in tightenco/overload
Package overload
Short Description Method overloading for PHP
License MIT
Homepage https://github.com/tightenco/overload
Informations about the package overload
Method overloading for PHP
NOTE: This is a beta release. It's Adam's original code almost exactly, and his docs; if a lot of folks are interested, we can, as a community, find its limits and edges and where it needs to grow. Please note that, while all credit goes to Adam for writing this, the responsibility for maintaining it is not on him. Tighten will do our best to keep it up, but if this goes anywhere it will be because of community support. This is a beta release and does not carry with it any promise that it doesn't have bugs or holes.
Installation
You can install the package via composer:
Usage
This package gives you a declarative way to support multiple signatures for the same method.
Basic Example
Say we have a Ticket
class with a holdUntil
method that lets us put that ticket on hold until a certain date and time by passing in a DateTime object:
...but now you decide it would be convenient if it could also accept a well-formatted date string.
Normally you'd do something like this:
The overloadable trait allows you to essentially pattern match in a declarative way instead of conditionally checking arguments:
If you wanted to avoid that duplication, you could even do this wild recursive madness:
A cooler example
You might be thinking:
"Uhhh bro, that looks like even more code."
Yeah, because that example is boring. This one is a bit more fun.
I've always wanted Laravel's validate
controller helper to accept a closure as its last parameter that let me return whatever HTTP response I wanted if validation failed.
But the method signature for validate
takes like a million things and I don't want to pass a ton of empty arrays, for example:
I'd love if I could just do:
...and have it magically work, knowing I clearly don't care about the $messages
or $customAttributes
arguments, but can you imagine how gross it would be to add those checks inside the validate
method to do all this argument counting and type checking?!
Check out how it would work with this badass trait from the gods:
Matching Options
Overloadable doesn't just work with closures; you can do all sorts of crazy stuff!
Check out this example from the test:
Methods are matched in the order they are specified when you call overload
.
Notes from Adam's original work
I'm still just hacking around with this and there's probably a bunch of things I'm missing.
For example, it just occurred to me that I haven't really considered how the reflection-based detection stuff should handle optional arguments, and off the top of my head I don't even know what it should do ¯\(ツ)/¯
Either way, I think it's some pretty fun code and I thought it was pretty cool that we could even come up with an API for it at all.
Upcoming plans:
Release beta with Adam's exact code- Discover known shortcomings and document as issues (for starters, optional arguments and the forthcoming union types)
- Fix ^^
- Profit? 🤣 OK, not profit.
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
The idea of method overloading comes from other languages that have it natively. I (Matt) have heard about it multiple times, including from my friend Adam Wathan, so when I decided to finally build something about it, I got a few hours in and then paused and asked Adam if he'd ever seen anyone build it. Turns out... he had.
He sent me a link to this gist. However, Adam didn't want to maintain a package, so, with his blessing, I spun this off to make it more accessible to the rest of the world.
- Adam Wathan
- Matt Stauffer
- All Contributors
License
The MIT License (MIT). Please see License File for more information.