grottopress / getter
直接获取私有对象的属性,无需调用getter方法。
v1.0.0
2023-05-31 15:18 UTC
Requires
- php: >=7.0
Requires (Dev)
README
描述
Getter 允许您直接获取 私有 对象属性,而无需调用getter方法。
例如,您可以直接使用 $myObject->myAttr
而不是调用 $myObject->getMyAttr()
。底层,Getter 通过调用您在类中定义的私有 getMyAttr()
方法来获取您的属性。
这样,您可以享受到语法糖的好处,同时保持封装性。
安装
使用composer安装:composer require grottopress/getter
使用方法
将特质导入您的类,并使用如下
<?php declare (strict_types = 1); namespace Vendor; use GrottoPress\Getter\GetterTrait; class MyClass { /** * Import trait */ use GetterTrait; private $haveMe; private $leaveMeAlone; public function __construct() { $this->haveMe = 'Hello :-)'; $this->leaveMeAlone = 'Go away!'; } /** * Define your private getter method * Method name should be of the format "get{$attrName}" */ private function getHaveMe(): string { return $this->haveMe; } // ... } // Instantiate $object = new Vendor\MyClass(); // Try to get attributes echo $object->haveMe; // => Hello :-) echo $object->leaveMeAlone; // => Error: 'getLeaveMeAlone()' not defined echo $object->nonExistent; // => Exception: 'nonExistent' does not exist
开发
使用composer run test
运行测试。
贡献
- 分支它
- 切换到
master
分支:git checkout master
- 创建您的功能分支:
git checkout -b my-new-feature
- 进行更改,根据需要更新更改日志和文档。
- 提交更改:
git commit
- 推送到分支:
git push origin my-new-feature
- 提交新的 拉取请求 到
GrottoPress:master
分支。