lpeltier / struct
Installs: 2 474
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.2
Requires (Dev)
- phpunit/phpunit: ^7.4
README
struct-like trait for PHP.
Usage
Create a struct by using the struct
trait in a class and declaring public
properties. You can then read and write those properties using the arrow (->
)
operator. Any attempt to access an undefined, protected, or private property
will result in an exception.
You can also use the constructor to set all properties at once.
Example:
<?php use lpeltier\Struct; class Foo { use Struct; public $foo = 'default'; public $bar; } $foo = new Foo(['bar' => 0x2A]); var_dump($foo->foo); // 'default' var_dump($foo->bar); // 42 var_dump($foo->baz); // throws an exception
Why
PHP automatically creates unknown properties when you set them, without even throwing a notice. This can lead to one those silly bugs that takes ages to find because you wrote 'nuw' instead of 'num'.