code-distortion / staticall
静态和非静态运行类方法
0.2.0
2024-03-23 02:38 UTC
Requires
- php: 7.0.* | 7.1.* | 7.2.* | 7.3.* | 7.4.* | 8.0.* | 8.1.* | 8.2.* | 8.3.*
Requires (Dev)
- infection/infection: ^0.10 | ^0.11 | ^0.12 | ^0.13 | ^0.14 | ^0.15 | ^0.16 | ^0.17 | ^0.18 | ^0.19 | ^0.20 | ^0.21 | ^0.22 | ^0.23 | ^0.24 | ^0.25 | ^0.26 | ^0.27
- phpstan/phpstan: ^0.9 | ^0.10 | ^0.11 | ^0.12 | ^1.0
- phpunit/phpunit: ~4.8 | ^5.0 | ^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0
- squizlabs/php_codesniffer: ^3.8.0
README
code-distortion/staticall 是一个允许您静态和非静态调用方法的包。
安装
使用 composer 安装包
composer require code-distortion/staticall
使用方法
- 在您的类中包含
Staticall特性 - 添加方法到您的类中,方法前缀为
staticall
<?php use CodeDistortion\Staticall\Staticall; // <<< class MyClass { use Staticall; // <<< private function staticallMyMethod(): string // <<< { return 'hello'; } }
MyClass::myMethod(); // "hello" $myObject = new MyClass(); $myObject->myMethod(); // "hello"
当以这种方式静态调用方法时,Staticall 会首先实例化类,然后调用该方法。
这对于具有可选链式方法且任何方法都可以首先调用的类很有用。
MyEmail::recipient('Bob', 'bob@test.com')->send(); MyEmail::attach('file.zip')->recipient('Bob', 'bob@test.com')->send();
注意: 因为 Staticall 会自动调用您的构造函数,所以构造函数不能有任何必需的参数。
注意: Staticall 会使其找到的方法公开访问。
您可以通过向您的类添加静态属性 $staticallPrefix 来更改方法前缀
<?php use CodeDistortion\Staticall\Staticall; class MyClass { use Staticall; protected static string $staticallPrefix = 'xyz'; // <<< private function xyzMyMethod(): string // <<< { return 'hello'; } }
测试此包
- 克隆此包:
git clone https://github.com/code-distortion/staticall.git . - 运行
composer install以安装依赖项 - 运行测试:
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
SemVer
此库使用 SemVer 2.0.0 版本控制。这意味着对 X 的更改表示破坏性更改:0.0.X、0.X.y、X.y.z。当此库更改到版本 1.0.0、2.0.0 等时,并不表示它是一个值得注意的发布版本,它仅仅表示更改是破坏性的。
实物
此包是 Treeware。如果您在生产环境中使用它,那么我们要求您为世界买一棵树以感谢我们的工作。通过为 Treeware 森林做出贡献,您将为当地家庭创造就业机会并恢复野生动物栖息地。
贡献
有关详细信息,请参阅 CONTRIBUTING。
行为准则
有关详细信息,请参阅 CODE_OF_CONDUCT。
安全
如果您发现任何安全问题,请通过电子邮件 tim@code-distortion.net 而不是使用问题跟踪器。
致谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 以获取更多信息。