keradus/traits

特性:通用用途特性

v1.0 2014-11-19 21:50 UTC

This package is auto-updated.

Last update: 2024-09-21 20:02:46 UTC


README

Latest Stable Version Latest Unstable Version Build status SensioLabsInsight

特性 - 通用用途特性。

特性列表

InaccessiblePropertiesProtectorTrait

特性,用于保护访问不可访问(未知)属性,有助于保持代码整洁和安全。

class Foo
{
    public $baz;
}

$foo = new Foo();
$foo->vaz = 123; // PHP claims that code is OK, even if you misspelled variable name!

class Bar
{
    use InaccessiblePropertiesProtectorTrait;

    public $baz;
}

$bar = new Bar();
$bar->vaz = 123; // now PHP throws \LogicException

InnerClassCacheTrait

特性,添加内部类缓存功能。

class Foo
{
    use InnerClassCacheTrait;

    public function square($x)
    {
        if (!isset($this->cache[$x])) {
            $this->cache[$x] = $x * $x;
        }

        return $this->cache[$x];
    }
}

InstanceCreationDisallowerTrait

特性,禁止创建类的实例。用于保护静态类(只有静态成员)。

class Foo
{
    use InstanceCreationDisallowerTrait;

    public static function createInstance()
    {
        return new static();
    }
}

$foo = new Foo(); // PHP throws fatal error
$foo = Foo::createInstance(); // PHP throws \LogicException

PropertyTrait

属性特性。

SingletonTrait

StaticPropertyTrait

以静态方式实现的属性特性。