lithium\analysis\Inspector::properties PHP Method

properties() public static method

Returns various information on the properties of an object.
public static properties ( mixed $class, array $options = [] ) : mixed
$class mixed A string class name or an object instance, from which to get methods.
$options array Set of options applied directly (check `_items()` for more options): - `'properties'`: array of properties to gather information from. - `'self'`: If true (default), only returns properties defined in `$class`, excluding properties from inherited classes.
return mixed Returns an array with information about the properties from the class given in $class or null on error.
    public static function properties($class, array $options = array())
    {
        $defaults = array('properties' => array(), 'self' => true);
        $options += $defaults;
        if (!(is_object($class) && $class instanceof ReflectionClass)) {
            try {
                $class = new ReflectionClass($class);
            } catch (ReflectionException $e) {
                return null;
            }
        }
        $options += array('names' => $options['properties']);
        return static::_items($class, 'getProperties', $options)->map(function ($item) {
            $class = __CLASS__;
            $modifiers = array_values($class::invokeMethod('_modifiers', array($item)));
            $setAccess = array_intersect($modifiers, array('private', 'protected')) !== array();
            if ($setAccess) {
                $item->setAccessible(true);
            }
            $result = compact('modifiers') + array('docComment' => $item->getDocComment(), 'name' => $item->getName(), 'value' => $item->getValue($item->getDeclaringClass()));
            if ($setAccess) {
                $item->setAccessible(false);
            }
            return $result;
        }, array('collect' => false));
    }

Usage Example

Example #1
0
 private function getMap($class)
 {
     $properties = Inspector::properties($class, array('public' => false));
     //parse out fields
     $fields = array();
     /** @var \ReflectionProperty $p */
     foreach ($properties as $p) {
         if (($p->getModifiers() & \ReflectionProperty::IS_PROTECTED) == \ReflectionProperty::IS_PROTECTED) {
             $name = $p->getName();
             if ($name[0] != '_') {
                 $fields[$name] = Docblock::comment($p->getDocComment());
             }
         }
     }
     //Parse out types
     $ret = array();
     foreach ($fields as $field => $data) {
         if (isset($data['tags']['var'])) {
             $ret[$field] = $this->dynamicType($data['tags']['var']);
         }
     }
     return $ret;
 }
All Usage Examples Of lithium\analysis\Inspector::properties