PHP/Class/Reflection Class Definition — различия между версиями

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

Текущая версия на 07:00, 26 мая 2010

Class information: is Abstract, is Final and is Instantiable

<?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 ReflectionClass( "Person" );
print classData( $p );
function classData( ReflectionClass $class ) {
  $details = "";
  $name = $class->getName();
  if ( $class->isAbstract() ) {
    $details .= "$name is an abstract class\n"; 
  }
  if ( $class->isFinal() ) {
    $details .= "$name is a final class\n"; 
  }
  if ( $class->isInstantiable() ) {
    $details .= "$name can be instantiated\n"; 
  } else {
    $details .= "$name can not be instantiated\n"; 
  }
  return $details;
}
?>



Class information: is User Defined, is Internal and is Interface

<?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 ReflectionClass( "Person" );
print classData( $p );
function classData( ReflectionClass $class ) {
  $details = "";
  $name = $class->getName();
  if ( $class->isUserDefined() ) {
    $details .= "$name is user defined\n"; 
  }
  if ( $class->isInternal() ) {
    $details .= "$name is built-in\n"; 
  }
  if ( $class->isInterface() ) {
    $details .= "$name is interface\n"; 
  }
  return $details;
}
?>



Get class information: file name, start line and end line

<?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 getClassSource( ReflectionClass $class ) {
    $path = $class->getFileName();
    $lines = @file( $path );
    $from = $class->getStartLine();
    $to   = $class->getEndLine();
    $len  = $to-$from+1;
    return implode( array_slice( $lines, $from-1, $len ));
  }
}
print ReflectionUtil::getClassSource( 
  new ReflectionClass( "Person" ) );
?>



Get class name

<?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;
    }
}
$p = new Person("A",10);
print get_class( $p );
if ( get_class( $p ) == "Person" ) {
    print "\$p is a Person object\n";
}
?>



get_class_vars: get class variables

<?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;
    }
}
print_r( get_class_vars( "Person" ) );
?>



get_declared_classes: get all defined class

<?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_declared_classes() );
?>



Use Reflection class to export class

<?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 ReflectionClass( "Person" );
Reflection::export( $p );

?>



var_dump a class

<?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;
    }
}
var_dump( new Person("A",10) );
?>