DynamicPropertyHelper
Simplifies and standardizes implementing dynamic properties.
The set() method makes it easy to set a bunch of properties at once. It is especially useful when creating an object from a row of data retrieved from the database.
The magic methods __set(), __get(), __isset(), and __unset() provided by this trait call customPropertySet(), customPropertyGet(), customPropertyIsset(), and customPropertyUnset(), respectively. This is so that classes that use this trait but need to create their own magic methods can still use the functionality that this trait provides.
Classes that implement this trait can optionally define a protected property named $prop_aliases, which sets alternative names for any arbitrary subset of the class's properties. Then this trait will automatically map those aliases to the properties' canonical names. This is useful if the names of database columns do not match those of the related property names, or if old versions of SMF used different names in different places for the same value.
For the most part, $prop_aliases is just a simple set of key-value pairs that take the form 'property_alias' => 'property_name'. However, there are three special cases to be aware of:
-
If '!' is prepended to the property_name, then the property_alias value will be treated like the boolean inverse of the real property's value.
For example, if $prop_aliases looks like this:
protected array $prop_aliases = array( 'is_logged' => '!is_guest', );
... then getting the value of the virtual $this->is_logged will get the opposite of the real $this->is_guest, and trying to set the value of $this->is_logged to true will actually just set $this->is_guest to false.
-
Square brackets in the property_name indicate that the alias should point to the value of an element within a property that is an array.
For example, if $prop_aliases looks like this:
protected array $prop_aliases = array( 'website_url' => 'website[url]', );
... then $this->website_url becomes an alias for $this->website['url'].
-
If property_name is the name of a callable, then getting the property's value will return the result of executing the callable. Attempts to set or unset a callable property are ignored. Checking whether the property is set will simply check whether the callable returns a non-null value.
For example, if $prop_aliases looks like this:
protected array $prop_aliases = array( 'foo' => __CLASS__ . '::getFoo', );
... then getting $this->foo will get the value returned by the class's getFoo() method. The callable must be a static method in order to keep PHP happy. The current object will be passed to the callable as its first argument.
Table of Contents
Properties
- $custom : array<string|int, mixed>
Methods
- __get() : mixed
- Gets custom property values.
- __isset() : bool
- Checks whether a custom property has been set.
- __set() : void
- Sets custom properties.
- __unset() : void
- Unsets custom properties.
- set() : void
- Sets an array of properties on this object.
- customPropertyGet() : mixed
- Gets custom property values.
- customPropertyIsset() : bool
- Checks whether a custom property has been set.
- customPropertySet() : void
- Sets custom properties.
- customPropertyUnset() : void
- Unsets custom properties.
Properties
$custom
protected
array<string|int, mixed>
$custom
= []
Arbitrary custom data about this object.
Methods
__get()
Gets custom property values.
public
__get(string $prop) : mixed
Parameters
- $prop : string
-
The property name.
__isset()
Checks whether a custom property has been set.
public
__isset(string $prop) : bool
Parameters
- $prop : string
-
The property name.
Return values
bool__set()
Sets custom properties.
public
__set(string $prop, mixed $value) : void
Parameters
- $prop : string
-
The property name.
- $value : mixed
-
The value to set.
__unset()
Unsets custom properties.
public
__unset(string $prop) : void
Parameters
- $prop : string
-
The property name.
set()
Sets an array of properties on this object.
public
set([array<string|int, mixed> $props = [] ]) : void
Parameters
- $props : array<string|int, mixed> = []
-
Array of properties to set.
customPropertyGet()
Gets custom property values.
protected
customPropertyGet(mixed $prop) : mixed
Parameters
- $prop : mixed
-
The property name.
customPropertyIsset()
Checks whether a custom property has been set.
protected
customPropertyIsset(mixed $prop) : bool
Parameters
- $prop : mixed
-
The property name.
Return values
boolcustomPropertySet()
Sets custom properties.
protected
customPropertySet(mixed $prop, mixed $value) : void
Parameters
- $prop : mixed
-
The property name.
- $value : mixed
-
The value to set.
customPropertyUnset()
Unsets custom properties.
protected
customPropertyUnset(mixed $prop) : void
Parameters
- $prop : mixed
-
The property name.