stormmore/method-overloader
最新稳定版本:1.0.1
Composer 安装命令:
composer require stormmore/method-overloader
包简介
README 文档
README
It's simple tool enabling overloading methods in convenient way by types and number of arguments like in other languages.
$userRepository = new UserRepository(); $userRepository->add('Michael','Jordan', 23); $userRepository->add('Michael Jordan', 23); $userRepository->add(new User("Michael", "Jordan", 23)); $userRepository->add(new Player("Michael", "Jordan", 23)); $userRepository->add(['fist_name' => 'Michael', 'last_name' => 'Jordan', 'number' => 23]);
Installation
composer require stormmore/method-overloader
Usage
Use register method to register overloaded methods with their arguments and run invoke.
In case of no proper methodInvalidArgumentException will be thrown or you can define default behaviour with onFailure callback.
use \Storm\MethodOverload\MethodOverloader; class UserRepository { public function add(mixed ...$args): void { $addMethodOverloader = MethodOverloader::create($this) ->register($this->addByFirstNameLastNameAndNumber(...),'string', 'string', 'int') ->register($this->adddByUser(...), User::Class) ->register($this->addByPlayer(...), Player::class) ->register($this->addByArray(...), 'array') ->register($this->addNyNameAndNumber(...), 'string', 'int') ->onFailure(function() { throw new MyCustomException(); }); $addMethodOverloader->invoke($args); } private function addByFirstNameLastNameAndNumber(): void { } private function addNyNameAndNumber(string name, int number): void { } private function addByUser(User $user): void { } private function addByPlayer(Player $player): void { } private function addByArray($array): void { } }
Supported types:
string, int, float, numeric, bool, array, resource, callable, object, mixed, user defined class.
Requiments
This library requires PHP 8.0 and above.
To use First class callabe syntax
$callable = $object->methodName(...);
you will need php 8.1 and above.
Author
Michał Czerski
If you have any question or ideas you want share with me contact me on GitHub.
License
StormMethodOverloader is licensed under MIT licence.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-01-10