PHPUnit_Util_Class::getStaticAttribute PHP Method

getStaticAttribute() public static method

This also works for attributes that are declared protected or private.
public static getStaticAttribute ( string $className, string $attributeName ) : mixed
$className string
$attributeName string
return mixed
    public static function getStaticAttribute($className, $attributeName)
    {
        if (!is_string($className)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
        }
        if (!class_exists($className)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name');
        }
        if (!is_string($attributeName)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
        }
        $class = new ReflectionClass($className);
        while ($class) {
            $attributes = $class->getStaticProperties();
            if (array_key_exists($attributeName, $attributes)) {
                return $attributes[$attributeName];
            }
            $class = $class->getParentClass();
        }
        throw new PHPUnit_Framework_Exception(sprintf('Attribute "%s" not found in class.', $attributeName));
    }

Usage Example

Example #1
0
 /**
  * Returns the value of an attribute of a class or an object.
  * This also works for attributes that are declared protected or private.
  *
  * @param  mixed   $classOrObject
  * @param  string  $attributeName
  * @return mixed
  * @throws PHPUnit_Framework_Exception
  */
 public static function readAttribute($classOrObject, $attributeName)
 {
     if (!is_string($attributeName)) {
         throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
     }
     if (is_string($classOrObject)) {
         if (!class_exists($classOrObject)) {
             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name');
         }
         return PHPUnit_Util_Class::getStaticAttribute($classOrObject, $attributeName);
     } else {
         if (is_object($classOrObject)) {
             return PHPUnit_Util_Class::getObjectAttribute($classOrObject, $attributeName);
         } else {
             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name or object');
         }
     }
 }