PHP/Class/Reflection Class Methods — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:00, 26 мая 2010
Содержание
- 1 Call class method dynamically
- 2 Class method info: is private, is public, is protected
- 3 Class method info: is static, is final, is constructor, return references
- 4 Class method info: is User Defined, is Internal, is Abstract
- 5 Get Class method: file name, start line and end line
- 6 get_class_methods
- 7 in_array
- 8 is_callable: is method callable
- 9 method_exists
Call class method dynamically
<?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
?>
Class method info: is private, is public, is protected
<?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;
}
?>
Class method info: is static, is final, is constructor, return references
<?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;
}
?>
Class method info: is User Defined, is Internal, is Abstract
<?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;
}
?>
Get Class method: 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 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 );
?>
get_class_methods
<?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" ) );
?>
in_array
<?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
}
?>
is_callable: is method callable
<?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
}
?>
method_exists
<?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
}
?>