PHP/Class/Reflection Constructor

Материал из Web эксперт
Версия от 10:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Get class constructor"s parameter information

   <source lang="html4strict">

<?php class Person {

   private $name;    
   private $age;    
   private $id;    
   function __construct( $name, $age ) {
       $this->name = $name;
       $this->age = $age;
   }
   function setId( $id ) {
       $this->id = $id;
   }
   
   function getId(){
       echo "get id method";    
   }
   
   function __clone() {
       $this->id = 0;
   }

}

class PersonHelper{

   function __construct( &$Person ) {
   }

} $prod_class = new ReflectionClass( PersonHelper ); $method = $prod_class->getMethod( "__construct" ); $params = $method->getParameters(); foreach ( $params as $param ) {

 print argData( $param );

} function argData( ReflectionParameter $arg ) {

 $details = "";
 $name  = $arg->getName();
 $class = $arg->getClass();
 if ( ! empty( $class )  ) {
   $classname = $class->getName();
   $details .= "\$$name must be a $classname object\n"; 
 }
 if ( $arg->allowsNull() ) {
   $details .= "\$$name can be null\n"; 
 }
 if ( $arg->isPassedByReference() ) {
   $details .= "\$$name is passed by reference\n"; 
 }
 return $details;

} ?>


      </source>