danielefavi / fluent-api
用于流畅API接口的类:您可以以静态和非静态方式调用相同的方法。
1.0.1
2022-01-15 06:09 UTC
Requires
- php: ^7.4|^8.0
README
PHP流畅API接口的包:您可以以静态和非静态方式调用方法。
安装
composer require danielefavi/fluent-api
用法
1) 在需要的地方导入 FluentApi 类。
use DanieleFavi\FluentApi\FluentApi;
2) 使用 FluentApi
类扩展您的类。
class FluentMath extends FluentApi { }
3) 创建可以以静态和非静态方式调用的方法。
使用下划线 _
声明可以静态或非静态调用的函数。
以下示例中,函数 add
和 subtract
可以以静态或非静态方式调用。
class FluentMath extends FluentApi { private $result = 0; protected function _add($num) { $this->result += $num; return $this; } protected function _subtract($num) { $this->result -= $num; return $this; } public function result() { return $this->result; } }
4) 使用示例
$res1 = FluentMath::add(5) ->add(3) ->subtract(2) ->add(8) ->result(); $res2 = FluentMath::subtract(1) ->add(10) ->result(); // $res1 equals to 14 // $res2 equals to 9