estaleiroweb / traits
EstaleiroWeb其他项目中使用的PHP特质
dev-main
2022-11-29 11:29 UTC
Requires
- php: >=5.4
This package is auto-updated.
Last update: 2024-09-29 06:25:56 UTC
README
EstaleiroWeb其他项目中使用的PHP特质
特质描述
特质调试
实现一个方法,从被调用的位置打印所有跟踪信息。
class MyClass { use EstaleiroWeb\Traits\Debug; public function __construct(){ $this->fnA(); } public function fnA(){ $this->fnB(); } public function fnB(){ $this->debug(); } } new MyClass;
特质 GetterAndSetter
自动重载 Getters 和 Setters 方法。
class MyClass { use EstaleiroWeb\Traits\GetterAndSetter; private $xValue; public function __construct($value=1){ $this->readonly=[ 'v1'=>$value, 'v2'=>$value, ]; $this->protect=[ 'v3'=>$value, ]; $this->v4=$value; } public function getV4(){ $this->xValue; } public function setV2($value){ $this->xValue=$value * 3; } public function setV4($value){ $this->xValue=$value * 3; } } $o=new MyClass; print "{$o->v1},{$o->v2},{$o->v3},{$o->v4}\n"; // 1,1,1,3 $o->v1=2; $o->v2=2; $o->v3=2; $o->v4=2; print "{$o->v1},{$o->v2},{$o->v3},{$o->v4}\n"; // 1,6,2,6
特质 GetterAndSetterRO
与 GetterAndSetter 相同,但只实现只读方法。
特质 LoadParameters
轻松按顺序加载参数。
特质 NetTools
实现一些网络工具方法
- nmap: 检查主机端口是否空闲
- getFreeRandomPort: 发现可用的端口。
- checkHost: 检查主机是否存在
- goURL: 重定向到另一个URL
- hostname: 获取主机名
特质 Options
实现中
特质 OverLoadElements
实现中
特质 SessionConfig
实现中
特质 Singleton
实现一个单例类
class MyClass { use EstaleiroWeb\Traits\Singleton; public $v; protected function __construct($v){ $this->v=$v; } } $a=new MyClass(1); // ERROR $b=MyClass::singleton(1); $c=MyClass::singleton(2); print "{$b->v},{$c->v}"; // 1,1 $b-v=3; print "{$b->v},{$c->v}"; // 3,3
特质 SingletonClass
与 Singleton 相同,但对于每个不同的类调用都会创建一个新的实例。
特质 SingletonKey
与 Singleton 相同,但对于每个不同的键传递都会创建一个新的实例。