PHP Doku:: The Reflection class - class.reflection.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenReflectionThe Reflection class

Ein Service von Reinhard Neidl - Webprogrammierung.

Reflection

<<Extending

Reflection::export>>


UnterSeiten:

The Reflection class

Einführung

The reflection class.

Klassenbeschreibung

Reflection {
/* Methoden */
public static void export ( Reflector $reflector [, string $return = false ] )
public static array getModifierNames ( int $modifiers )
}

Inhaltsverzeichnis


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
apmithani at yahoo dot com
26.08.2010 18:05
Here is a code snippet for some of us who are just beginning with reflection. I have a simple class below with two properties and two methods. We will use reflection classes to populate the properties dynamically and then print them:

<?php

class A
{
    public
$one = '';
    public
$two = '';
   
   
//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();

$i =1;
//Now go through the $properties array and populate each property
foreach($properties as $property)
{
   
//Populating properties
   
$a->{$property->getName()}=$i;
   
//Invoking the method to print what was populated
   
$a->{"echo".ucfirst($property->getName())}()."\n";
   
   
$i++;
}

?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)