function reflectProperty($class, $property)
{
    $propInfo = new ReflectionProperty($class, $property);
    echo "**********************************\n";
    echo "Reflecting on property {$class}::{$property}\n\n";
    echo "__toString():\n";
    var_dump($propInfo->__toString());
    echo "export():\n";
    var_dump(ReflectionProperty::export($class, $property, true));
    echo "export():\n";
    var_dump(ReflectionProperty::export($class, $property, false));
    echo "getName():\n";
    var_dump($propInfo->getName());
    echo "isPublic():\n";
    var_dump($propInfo->isPublic());
    echo "isPrivate():\n";
    var_dump($propInfo->isPrivate());
    echo "isProtected():\n";
    var_dump($propInfo->isProtected());
    echo "isStatic():\n";
    var_dump($propInfo->isStatic());
    $instance = new $class();
    if ($propInfo->isPublic()) {
        echo "getValue():\n";
        var_dump($propInfo->getValue($instance));
        $propInfo->setValue($instance, "NewValue");
        echo "getValue() after a setValue():\n";
        var_dump($propInfo->getValue($instance));
    }
    echo "\n**********************************\n";
}
<?php

class TestClass
{
}
$a = 5;
echo "Non-existent class:\n";
try {
    ReflectionProperty::export("NonExistentClass", "prop", true);
} catch (Exception $e) {
    echo $e->getMessage();
}
echo "\n\nWrong property parameter type:\n";
try {
    ReflectionProperty::export($a, 'TestClass', false);
} catch (ReflectionException $e) {
    echo $e->getMessage();
}
echo "\n\nNon-existent property:\n";
try {
    ReflectionProperty::export('TestClass', "nonExistentProperty", true);
} catch (Exception $e) {
    echo $e->getMessage();
}
echo "\n\nIncorrect number of args:\n";
ReflectionProperty::export();
ReflectionProperty::export('TestClass', "nonExistentProperty", true, false);
<?php

class Foo
{
    public $prop;
    function Func($name)
    {
        echo "Hello {$name}";
    }
}
ReflectionClass::export('Foo');
//output: Class [ class Foo ] { @@ /home/cg/root/main.php 3-10 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [1] { Property [ public $prop ] } - Methods [1] { Method [ public method Func ] { @@ /home/cg/root/main.php 7 - 9 - Parameters [1] { Parameter #0 [ $name ] } } } }
ReflectionObject::export(new Foo());
// output: Object of class [ class Foo ] { @@ /home/cg/root/main.php 3-10 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [1] { Property [ public $prop ] } - Dynamic properties [0] { } - Methods [1] { Method [ public method Func ] { @@ /home/cg/root/main.php 7 - 9 - Parameters [1] { Parameter #0 [ $name ] } } } }
ReflectionMethod::export('Foo', 'func');
// output:  Method [ public method Func ] { @@ /home/cg/root/main.php 7 - 9 - Parameters [1] { Parameter #0 [ $name ] } }
ReflectionProperty::export('Foo', 'prop');
//output: Property [ public $prop ]
ReflectionExtension::export('standard');
//output: Extension [ extension #15 standard version 5.5.18 ] ...
 /**
  * Tests export.
  */
 public function testToString()
 {
     $tests = array('lines', 'docComment', 'noComment', 'default', 'typeNull', 'typeArray', 'typeString', 'typeInteger', 'typeFloat');
     foreach ($tests as $test) {
         $rfl = $this->getPropertyReflection($test);
         $this->assertSame($rfl->internal->__toString(), $rfl->token->__toString());
         $this->assertSame(InternalReflectionProperty::export($this->getClassName($test), $test, true), ReflectionProperty::export($this->getBroker(), $this->getClassName($test), $test, true));
         // Test loading from a string
         $rfl = $this->getPropertyReflection($test, true);
         $this->assertSame($rfl->internal->__toString(), $rfl->token->__toString());
     }
     $rfl = $this->getClassReflection('modifiers');
     $rfl_fromString = $this->getClassReflection('modifiers');
     foreach (array('public', 'protected', 'private') as $name) {
         $internal = $rfl->internal->getProperty($name);
         $token = $rfl->token->getProperty($name);
         $this->assertSame($internal->__toString(), $token->__toString());
         $this->assertSame(InternalReflectionProperty::export($this->getClassName('modifiers'), $name, true), ReflectionProperty::export($this->getBroker(), $this->getClassName('modifiers'), $name, true));
         // Test loading from a string
         $this->assertSame($internal->__toString(), $rfl_fromString->token->getProperty($name)->__toString());
     }
     $this->assertSame(InternalReflectionProperty::export('ReflectionProperty', 'name', true), ReflectionProperty::export($this->getBroker(), 'ReflectionProperty', 'name', true));
     $this->assertSame(InternalReflectionProperty::export(new InternalReflectionProperty('ReflectionProperty', 'name'), 'name', true), ReflectionProperty::export($this->getBroker(), new InternalReflectionProperty('ReflectionProperty', 'name'), 'name', true));
 }
<?php

class TestClass
{
    public $proper = 5;
}
var_dump(ReflectionProperty::export('TestClass', 'proper'));
Example #6
0
 /**
  * Exports a ReflectionProperty instance.
  *
  * Returns the output if TRUE is specified for return, printing it otherwise.
  * This is purely a wrapper method, which calls the corresponding method of
  * the parent class.
  * @param ReflectionClass|string $class
  *        ReflectionClass object or name of the class
  * @param ReflectionProperty|string $property
  *        ReflectionProperty object or name of the class
  * @param boolean $return
  *        Whether to return (TRUE) or print (FALSE) the output
  * @return mixed
  */
 public static function export($class, $property, $return = false)
 {
     return parent::export($class, $property, $return);
 }