PHP公式の例です

1136 ワード

<?php
class A
{
    public $one = 'a';
    public $two = 'b';
    
    //Constructor
    public function __construct()
    {
        //Constructor
    }
    
    //print variable one
    public function echoOne()
    {
        echo $this->one."
"; } //print variable two public function echoTwo() { echo $this->two."
"; } } //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())}()."
"; $i++; } echo "</pre>";