Traits
, (*1)
Traits - general usage traits., (*2)
Traits List
InaccessiblePropertiesProtectorTrait
Trait, that protects accessing inaccessible (unknown) properties to help keeping code clean and safe., (*3)
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., (*4)
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)., (*5)
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., (*6)
SingletonTrait
StaticPropertyTrait
Property trait in static way., (*7)