keradus/traits

Traits: general usage traits

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

This package is auto-updated.

Last update: 2024-10-21 20:05:52 UTC


README

Latest Stable Version Latest Unstable Version Build status SensioLabsInsight

Traits - general usage traits.

Traits List

InaccessiblePropertiesProtectorTrait

Trait, that protects accessing inaccessible (unknown) properties to help keeping code clean and safe.

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

Trait, that adds functionality of inner class cache.

class Foo
{
    use InnerClassCacheTrait;

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

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

InstanceCreationDisallowerTrait

Trait, that disallow to create instance of class. Use for protecting static classes (only static members).

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

Property trait.

SingletonTrait

StaticPropertyTrait

Property trait in static way.