\RedBeanPHP\UtilEither

Either Utility

The Either Utility class provides an easy way to substitute the NULL coalesce operator in RedBeanPHP (since the lazy loading interface interferes with the ?? operator) in a way that can also be used in older PHP-versions.

Summary

Methods
Properties
Constants
__construct()
__get()
first()
last()
index()
_or()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$result
N/A

Properties

$result

$result : mixed

Type

mixed

Methods

__construct()

__construct(  $result) 

Constructs a new Either-instance.

Example usage:

$author = $text ->either() ->page ->book ->autor ->name ->or('unknown');

The Either-class lets you access bean properties without having to do NULL-checks. The mechanism resembles the use of the ?? somewhat but offers backward compatibility with older PHP versions. The mechanism also works on arrays.

$budget = $company ->either() ->sharedProject ->first() ->budget ->or(0);

Parameters

$result

__get()

__get(string  $something) : self

Extracts a value from the wrapped object and stores it in the internal result object. If the desired value cannot be found, the internal result object will be set to NULL. Chainable.

Parameters

string $something

name of the property you wish to extract the value of

Returns

self

first()

first() : self

Extracts the first element of the array in the internal result object and stores it as the new value of the internal result object.

Chainable.

Returns

self

last()

last() : self

Extracts the last element of the array in the internal result object and stores it as the new value of the internal result object.

Chainable.

Returns

self

index()

index(  $key) : self

Extracts the specified element of the array in the internal result object and stores it as the new value of the internal result object.

Chainable.

Parameters

$key

Returns

self

_or()

_or(mixed  $value) : mixed

Resolves the Either-instance to a final value, either the value contained in the internal result object or the value specified in the or() function.

Parameters

mixed $value

value to resolve to if internal result equals NULL

Returns

mixed