PHP/Class/Reflection Class Methods

Материал из Web эксперт
Перейти к: навигация, поиск

Call class method dynamically

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

} $p = new Person("A",10); $method = "getId"; // define a method name print $p->$method(); // invoke the method ?>


      </source>
   
  


Class method info: is private, is public, is protected

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

} $prod_class = new ReflectionClass( "Person" ); $methods = $prod_class->getMethods(); foreach ( $methods as $method ) {

 print methodData( $method );
 print "\n----\n";

} function methodData( ReflectionMethod $method ) {

 $details = "";
 $name = $method->getName();
 if ( $method->isPublic() ) {
   $details .= "$name is public\n"; 
 }
 if ( $method->isProtected() ) {
   $details .= "$name is protected\n"; 
 }
 if ( $method->isPrivate() ) {
   $details .= "$name is private\n"; 
 }
 return $details;

} ?>

      </source>
   
  


Class method info: is static, is final, is constructor, return references

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

} $prod_class = new ReflectionClass( "Person" ); $methods = $prod_class->getMethods(); foreach ( $methods as $method ) {

 print methodData( $method );
 print "\n----\n";

} function methodData( ReflectionMethod $method ) {

 $details = "";
 $name = $method->getName();
 if ( $method->isStatic() ) {
   $details .= "$name is static\n"; 
 }
 if ( $method->isFinal() ) {
   $details .= "$name is final\n"; 
 }
 if ( $method->isConstructor() ) {
   $details .= "$name is the constructor\n"; 
 }
 if ( $method->returnsReference() ) {
   $details .= "$name returns a reference (as opposed to a value)\n"; 
 }
 return $details;

} ?>


      </source>
   
  


Class method info: is User Defined, is Internal, is Abstract

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

} $prod_class = new ReflectionClass( "Person" ); $methods = $prod_class->getMethods(); foreach ( $methods as $method ) {

 print methodData( $method );
 print "\n----\n";

} function methodData( ReflectionMethod $method ) {

 $details = "";
 $name = $method->getName();
 if ( $method->isUserDefined() ) {
   $details .= "$name is user defined\n"; 
 }
 if ( $method->isInternal() ) {
   $details .= "$name is built-in\n"; 
 }
 if ( $method->isAbstract() ) {
   $details .= "$name is abstract\n"; 
 }
 return $details;

} ?>

      </source>
   
  


Get Class method: file name, start line and end line

   <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 ReflectionUtil {

 static function getMethodSource( ReflectionMethod $method ) {
   $path = $method->getFileName();
   $lines = @file( $path );
   $from = $method->getStartLine();
   $to   = $method->getEndLine();
   $len  = $to-$from+1;
   return implode( array_slice( $lines, $from-1, $len ));
 }

} $class = new ReflectionClass( "Person" ); $method = $class->getMethod( "getId" ); print ReflectionUtil::getMethodSource( $method ); ?>


      </source>
   
  


get_class_methods

   <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 __clone() {
       $this->id = 0;
   }

} print_r( get_class_methods( "Person" ) );

?>


      </source>
   
  


in_array

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

} $p = new Person("A",10); $method = "getId"; // define a method name if ( in_array( $method, get_class_methods( $p ) ) ) {

   print $p->$method();  // invoke the method

} ?>

      </source>
   
  


is_callable: is method callable

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

} $p = new Person("A",10); $method = "getId"; // define a method name if ( is_callable( array( get_class($p), $method) ) ) {

   print $p->$method();  // invoke the method

} ?>


      </source>
   
  


method_exists

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

} $p = new Person("A",10);

$method = "getId"; // define a method name

if ( method_exists( $p, $method ) ) {

   print $p->$method();  // invoke the method

} ?>


      </source>