FilterProperties::from()

Not yet in a tagged release

Description

FilterProperties::from() - get the properties from a class or object

// remember to import first
use GanbaroDigital\MissingBits\ClassesAndObjects\FilterProperties;

// our method signature
array FilterProperties::from(ReflectionClass $refObj, int $propTypes, callable $resultFilter);

Parameters

FilterProperties::from() takes three parameters:

The Result Filter

The $resultFilter parameter is a callable. It must have this function signature:

boolean function(ReflectionProperty $refProp, array &$finalResult);

where:

FilterProperties::from() will call $resultFilter() with each discovered property. Your $resultFilter callable is responsible for storing the property in $finalResult.

Only properties stored in $finalResult will be returned by FilterProperties::from()!

For example:

// in this example, $target is the object that we want to filter
$resultFilter = function(ReflectionProperty $refProp, &$finalResult) use($target) {
    // only store object properties
    if ($refProp->isStatic()) {
        return;
    }

    // store the property to be returned to the caller
    $knownProperties[$refProp->getName()] = $refProp->getValue($target);
};

Return Values

FilterProperties::from() returns an array of name / value pairs. The exact contents of this return value is decided by your $resultFilter callable.

Throws

FilterProperties::from() does not throw any exceptions.

Notes

None at this time.