agorlov/php-constructor-overloading
Composer 安装命令:
composer require agorlov/php-constructor-overloading
包简介
Multiple constructors in PHP
README 文档
README
Constructor overload PHP methods, like in Java or C++
Based on article (how to adopt this technic from Java to PHP):
https://www.yegor256.com/2015/05/28/one-primary-constructor.html
For PHP > 7
How to install
$ composer require agorlov/php-constructor-overloading
Usage example
use AG\OverloadedConstructor; final class Cash { /** @var int */ private $cents; /** @var string */ private $currency; private function constrEmpty() { // secondary $this->__construct(0); } private function constrCnts(int $cts) { // secondary $this->__construct($cts, "USD"); } private function constrPrimary(int $cts, string $crn) { // primary $this->cents = $cts; $this->currency = $crn; } public function __construct() { // overloaded constructors invocation $method = (new OverloadedConstructor($this, func_get_args()))->constructor(); $this->$method(...func_get_args()); } // methods here } var_dump(new Cash());
more examples: in example.php and example2.php
Valid scalar hints
- array
- callable
- bool not boolean!
- float not double!
- int not integer!
- string
- iterable
- object
Tests
$ composer install $ vendor/bin/phpunit OverloadedConstructorTest.php
统计信息
- 总下载量: 40
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-16