Library
Class

Library\Object\RegistryInvokable

class RegistryInvokable implements InvokableInterface

Magic handling of properties access

Presentation

This model will store any property dynamically set in the $_data registry:

$obj->prop = value => $_data[prop] = value
$obj->setProp(value) => $_data[prop] = value

Depending on the flag of the object, to access the property, use:

echo $obj->prop // if flag=PUBLIC_PROPERTIES
echo $obj->getProp() // if flag=PROTECTED_PROPERTIES

Be very careful about the properties names as they will all be transformed in lower case and underscore:

$obj->myPropertyName => $_data[ my_property_name ]
$obj->myProperty_name => $_data[ my_property_name ]

$obj->setMyPropertyName => $_data[ my_property_name ]
$obj->setMyProperty_name => $_data[ my_property_name ]

Flags

Three constants flags are defined in the class to let you choose the visibility of the object properties:

Default flag of the class is PROTECTED_PROPERTIES to force the magic getter usage and allow you to override these methods in your child class if necessary, without worrying about method existence.

Rules

All setter methods returns the object itself for chainability.

Constants

PUBLIC_PROPERTIES

Defines all registry properties as public

PROTECTED_PROPERTIES

Defines all registry properties as protected

UNAUTHORIZED_PROPERTIES

Defines all registry properties as protected and throw an error if an access is done

Methods

__construct(array $data = null, int $flag = self::PROTECTED_PROPERTIES)

Object constructor

RegistryInvokable setFlag(int $flag)

Set the object flag for registry properties visibility

int getFlag()

Get the object flag for registry properties visibility

mixed __call(string $name, array $arguments)

Magic handler when calling a non-existing method on an object

static null __callStatic(string $name, array $arguments)

Avoiding magic static handler

mixed __get(string $name)

Magic getter

RegistryInvokable __set(string $name, mixed $value)

Magic setter

bool __isset(string $name)

Magic checker

RegistryInvokable __unset(string $name)

Magic unsetter

mixed getData(string $name = null, mixed $default = null)

Global getter

mixed setData($value, $arg2 = null)

Global setter

Details

at line 115
public __construct(array $data = null, int $flag = self::PROTECTED_PROPERTIES)

Object constructor

Parameters

array $data The object data to load in registry (optional)
int $flag The object flag (one of the class constants - default is self::PROTECTED_PROPERTIES)

at line 130
public RegistryInvokable setFlag(int $flag)

Set the object flag for registry properties visibility

Parameters

int $flag Must be one of the class constants

Return Value

RegistryInvokable

at line 141
public int getFlag()

Get the object flag for registry properties visibility

Return Value

int The object flag value

at line 162
public mixed __call(string $name, array $arguments)

Magic handler when calling a non-existing method on an object

Magic method handling getProp(default), setProp(value), unsetProp(), issetProp() or resetProp().

Warning: the reset function here is the same as unset (not a real resetting to the original value).

Parameters

string $name The non-existing method name called on the object
array $arguments The arguments array passed calling the method

Return Value

mixed This will return the result of a magic method, or nothing if nothing can be done

See also

<http://www.php.net/manual/en/language.oop5.overloading.php>
Patterns\Commons\Registry::getEntry()
Patterns\Commons\Registry::setEntry()
Patterns\Commons\Registry::isEntry()

at line 201
static public null __callStatic(string $name, array $arguments)

Avoiding magic static handler

Parameters

string $name The non-existing method name called on the object
array $arguments The arguments array passed calling the method

Return Value

null Must return the result of a magic method, or nothing if nothing can be done

at line 221
public mixed __get(string $name)

Magic getter

Magic method called when getProp(arg, default) or $this->prop are invoked. If the object flag is set on self::PROTECTED_PROPERTIES, this will return null ; if the flag is set on self::UNAUTHORIZED_PROPERTIES, an exception is thrown.

Parameters

string $name The name of the property to get

Return Value

mixed This will return the result of a magic method, or nothing if nothing can be done

Exceptions

InvokableAccessException Throws an InvokableAccessException if direct access to properties is avoid by the self::UNAUTHORIZED_PROPERTIES flag

See also

<http://www.php.net/manual/en/language.oop5.overloading.php>
Patterns\Commons\Registry::getEntry()

at line 244
public RegistryInvokable __set(string $name, mixed $value)

Magic setter

Magic method called when setProp(arg, value) or $this->arg = value are invoked.

Parameters

string $name The name of the property to get
mixed $value The value to set for the property

Return Value

RegistryInvokable Returns $this for method chaining

See also

<http://www.php.net/manual/en/language.oop5.overloading.php>
Patterns\Commons\Registry::setEntry()

at line 261
public bool __isset(string $name)

Magic checker

Magic method called when issetProp(), isset($this->prop) or empty($this->prop) are invoked.

Parameters

string $name The name of the property to get

Return Value

bool This will return true if the property exists, false otherwise

See also

<http://www.php.net/manual/en/language.oop5.overloading.php>
Patterns\Commons\Registry::isEntry()

at line 277
public RegistryInvokable __unset(string $name)

Magic unsetter

Magic method called when unsetProp() or unset($this->prop) are invoked.

Parameters

string $name The name of the property to get

Return Value

RegistryInvokable Returns $this for method chaining

See also

<http://www.php.net/manual/en/language.oop5.overloading.php>
Patterns\Commons\Registry::_invokeUnset()

at line 293
public mixed getData(string $name = null, mixed $default = null)

Global getter

Get the value of a registry property or the whole registry array.

Parameters

string $name The name of a property to get if so (optional)
mixed $default A default value to send if the property doesn't exist in the object

Return Value

mixed The value of the property if so, the default value if the property doesn't exist, or the flobal data array without a property name

at line 314
public mixed setData($value, $arg2 = null)

Global setter

Set the value of a registry property or the whole registry array.

Parameters

$value
$arg2

Return Value

mixed The value of the property if so, the default value if the property doesn't exist, or the flobal data array without a property name

Exceptions

InvalidArgumentExcpetion Throws an InvalidArgumentExcpetion if $name is null and $value is not an array