一个PHP官方的例子

<?php
class A
{
    public $one = 'a';
    public $two = 'b';
    
    //Constructor
    public function __construct()
    {
        //Constructor
    }
    
    //print variable one
    public function echoOne()
    {
        echo $this->one."\n";
    }
    //print variable two    
    public function echoTwo()
    {
        echo $this->two."\n";
    }
}
//Instantiate the object
$a = new A();
//Instantiate the reflection object
$reflector = new ReflectionClass('A');
//Now get all the properties from class A in to $properties array
$properties = $reflector->getProperties();
echo "<pre>";
//var_dump($properties);
$i =1;
//Now go through the $properties array and populate each property
foreach($properties as $property)
{
// var_dump($property->getName());
// continue;
    //Populating properties
    $a->{$property->getName()}=$i;
    //Invoking the method to print what was populated
    $a->{"echo".ucfirst($property->getName())}()."\n";
    
    $i++;
}
echo "</pre>";

你可能感兴趣的:(一个PHP官方的例子)