Usage
Table of contents
There are different ways of using this package which will be outlined further down. Choose whatever suits your needs better.
Using Inheritance
The most simple way to use the package is by extending the HandyObject class:
use NorseBlue\HandyProperties\HandyObject;
class MyObject extends HandyObject
{
}
Using Composition
If you prefer composition over inheritance, you can use the provided traits instead:
use NorseBlue\HandyProperties\Traits\HasPropertyAccessors;
use NorseBlue\HandyProperties\Traits\HasPropertyMutators;
class MyObject
{
use HasPropertyAccessors;
use HasPropertyMutators;
}
Defining Accessors
Accessors are ways to retrieve values from your objects: $my_object->value.
To define an accessor for your object you have to define a function with a name that follows the established convention:
[final] <visibility> function accessor<PropertyName>(): <return_type> { ... }
- The method can have the
finalmodifier applied to it. - The method can have any visibility given to it (
private,protectedorpublic). When using inheritance, aprivatemethod won’t be accessible to the parent class, so useprotectedinstead. - The accessor method name must be prefixed with the word
accessor. - The last part of the method name defines the property name. The property can be accessed through both the camel case or the snake case name (e.g.
myPropertyormy_property). It is encouraged to stick with just one style throughout your code. - You can (and should) specify the return type of the accessor.
- You are encouraged to use docblocks to define your object’s accessors for your IDE auto-completion feature (using
@property-readif they aren’t also mutable or@propertyif they are).
Defining Mutators
Mutators are ways to modify values within your objects: $my_object->value = 'new value'.
To define a mutator for your object you have to define a function with a name that follows the established convention:
[final] <visibility> function mutator<PropertyName>(): <return_type> { ... }
- The method can have the
finalmodifier applied to it. - The method can have any visibility given to it (
private,protectedorpublic). When using inheritance, aprivatemethod won’t be accessible to the parent class, so useprotectedinstead. - The mutator method name must be prefixed with the word
mutator. - The last part of the method name defines the property name. The property can be accessed through both the camel case or the snake case name (e.g.
myPropertyormy_property). It is encouraged to stick with just one style throughout your code. - The return type will never bee returned when using the property syntax (e.g.
$object->myProperty = 'value') but you are able to define a return type other thanvoidif you call the method directly. - You are encouraged to use docblocks to define your object’s mutators for your IDE auto-completion feature (using
@property-writeif they aren’t also accessible or@propertyif they are).