PHP面向对象程序设计之类与反射API详解

2019-05-02 11:20:10王冬梅

output:

CdProduct is user defined
CdProduct can be instantiated

查看方法数据

<?php
require_once "fullshop.php";
$prod_class = new ReflectionClass( 'CdProduct' );
$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 definedn";
 }
 if ( $method->isInternal() ) {
  $details .= "$name is built-inn";
 }
 if ( $method->isAbstract() ) {
  $details .= "$name is abstractn";
 }
 if ( $method->isPublic() ) {
  $details .= "$name is publicn";
 }
 if ( $method->isProtected() ) {
  $details .= "$name is protectedn";
 }
 if ( $method->isPrivate() ) {
  $details .= "$name is privaten";
 }
 if ( $method->isStatic() ) {
  $details .= "$name is staticn";
 }
 if ( $method->isFinal() ) {
  $details .= "$name is finaln";
 }
 if ( $method->isConstructor() ) {
  $details .= "$name is the constructorn";
 }
 if ( $method->returnsReference() ) {
  $details .= "$name returns a reference (as opposed to a value)n";
 }
 return $details;
}
?>

output:

__construct is user defined
__construct is public
__construct is the constructor
----
getPlayLength is user defined
getPlayLength is public
----
getSummaryLine is user defined
getSummaryLine is public
----
getProducerFirstName is user defined
getProducerFirstName is public
----
getProducerMainName is user defined
getProducerMainName is public
----
setDiscount is user defined
setDiscount is public
----
getDiscount is user defined
getDiscount is public
----
getTitle is user defined
getTitle is public
----
getPrice is user defined
getPrice is public
----
getProducer is user defined
getProducer is public

获取构造函数参数情况

<?php
require_once "fullshop.php";
$prod_class = new ReflectionClass( 'CdProduct' );
$method = $prod_class->getMethod( "__construct" );
$params = $method->getParameters();
foreach ( $params as $param ) {
  print argData( $param )."n";
}
function argData( ReflectionParameter $arg ) {
 $details = "";
 $declaringclass = $arg->getDeclaringClass();
 $name = $arg->getName();
 $class = $arg->getClass();
 $position = $arg->getPosition();
 $details .= "$$name has position $positionn";
 if ( ! empty( $class ) ) {
  $classname = $class->getName();
  $details .= "$$name must be a $classname objectn";
 }
 if ( $arg->isPassedByReference() ) {
  $details .= "$$name is passed by referencen";
 }
 if ( $arg->isDefaultValueAvailable() ) {
  $def = $arg->getDefaultValue();
  $details .= "$$name has default: $defn";
 }
 if ( $arg->allowsNull() ) {
  $details .= "$$name can be nulln";
 }
 return $details;
}
?>

output:

$title has position 0
$title can be null
$firstName has position 1
$firstName can be null
$mainName has position 2
$mainName can be null
$price has position 3
$price can be null
$playLength has position 4
$playLength has default: 78
$playLength can be null

								 
			 
相关文章 大家在看