Get class constructor's parameter information : Reflection Constructor « Class « PHP






Get class constructor's parameter information

<?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;
}

?>


           
       








Related examples in the same category