反射api
fullshop.php
<?php
class shopproduct {
  private $title;
  private $producermainname;
  private $producerfirstname;
  protected $price;
  private $discount = 0;
  public function construct(  $title, $firstname,
              $mainname, $price ) {
    $this->title       = $title;
    $this->producerfirstname = $firstname;
    $this->producermainname = $mainname;
    $this->price       = $price;
  }
  public function getproducerfirstname() {
    return $this->producerfirstname;
  }
  public function getproducermainname() {
    return $this->producermainname;
  }
  public function setdiscount( $num ) {
    $this->discount=$num;
  }
  public function getdiscount() {
    return $this->discount;
  }
  public function gettitle() {
    return $this->title;
  }
  public function getprice() {
    return ($this->price - $this->discount);
  }
  public function getproducer() {
    return "{$this->producerfirstname}".
        " {$this->producermainname}";
  }
  public function getsummaryline() {
    $base = "{$this->title} ( {$this->producermainname}, ";
    $base .= "{$this->producerfirstname} )";
    return $base;
  }
}
class cdproduct extends shopproduct {
  private $playlength = 0;
  public function construct(  $title, $firstname,
              $mainname, $price, $playlength=78 ) {
    parent::construct(  $title, $firstname,
                $mainname, $price );
    $this->playlength = $playlength;
  }
  public function getplaylength() {
    return $this->playlength;
  }
  public function getsummaryline() {
    $base = parent::getsummaryline();
    $base .= ": playing time - {$this->playlength}";
    return $base;
  }
}
class bookproduct extends shopproduct {
  private $numpages = 0;
  public function construct(  $title, $firstname,
              $mainname, $price, $numpages ) {
    parent::construct(  $title, $firstname,
                $mainname, $price );
    $this->numpages = $numpages;
  }
  public function getnumberofpages() {
    return $this->numpages;
  }
  public function getsummaryline() {
    $base = parent::getsummaryline();
    $base .= ": page count - {$this->numpages}";
    return $base;
  }
  public function getprice() {
    return $this->price;
  }
}
/*
$product1 = new cdproduct("cd1", "bob", "bobbleson", 4, 50 );
print $product1->getsummaryline()."\n";
$product2 = new bookproduct("book1", "harry", "harrelson", 4, 30 );
print $product2->getsummaryline()."\n";
*/
?>
<?php
require_once "fullshop.php";
$prod_class = new reflectionclass( 'cdproduct' );
reflection::export( $prod_class );
?>
output:
class [ <user> class cdproduct extends shopproduct ] {
 @@ d:\xampp\htdocs\popp-code\5\fullshop.php 53-73
 - constants [0] {
 }
 - static properties [0] {
 }
 - static methods [0] {
 }
 - properties [2] {
  property [ <default> private $playlength ]
  property [ <default> protected $price ]
 }
 - methods [10] {
  method [ <user, overwrites shopproduct, ctor> public method construct ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 56 - 61
   - parameters [5] {
    parameter #0 [ <required> $title ]
    parameter #1 [ <required> $firstname ]
    parameter #2 [ <required> $mainname ]
    parameter #3 [ <required> $price ]
    parameter #4 [ <optional> $playlength = 78 ]
   }
  }
  method [ <user> public method getplaylength ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 63 - 65
  }
  method [ <user, overwrites shopproduct, prototype shopproduct> public method getsummaryline ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 67 - 71
  }
  method [ <user, inherits shopproduct> public method getproducerfirstname ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 17 - 19
  }
  method [ <user, inherits shopproduct> public method getproducermainname ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 21 - 23
  }
  method [ <user, inherits shopproduct> public method setdiscount ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 25 - 27
   - parameters [1] {
    parameter #0 [ <required> $num ]
   }
  }
  method [ <user, inherits shopproduct> public method getdiscount ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 29 - 31
  }
  method [ <user, inherits shopproduct> public method gettitle ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 33 - 35
  }
  method [ <user, inherits shopproduct> public method getprice ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 37 - 39
  }
  method [ <user, inherits shopproduct> public method getproducer ] {
   @@ d:\xampp\htdocs\popp-code\5\fullshop.php 41 - 44
  }
 }
}
点评:把类看的透彻的一塌糊涂,比var_dump强多了。哪些属性,继承了什么类。类中的方法哪些是自己的,哪些是重写的,哪些是继承的,一目了然。
查看类数据
<?php
require_once("fullshop.php");
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";
 }
 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;
}
$prod_class = new reflectionclass( 'cdproduct' );
print classdata( $prod_class );
?>
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 defined\n";
 }
 if ( $method->isinternal() ) {
  $details .= "$name is built-in\n";
 }
 if ( $method->isabstract() ) {
  $details .= "$name is abstract\n";
 }
 if ( $method->ispublic() ) {
  $details .= "$name is public\n";
 }
 if ( $method->isprotected() ) {
  $details .= "$name is protected\n";
 }
 if ( $method->isprivate() ) {
  $details .= "$name is private\n";
 }
 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;
}
?>
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 $position\n";
 if ( ! empty( $class ) ) {
  $classname = $class->getname();
  $details .= "\$$name must be a $classname object\n";
 }
 if ( $arg->ispassedbyreference() ) {
  $details .= "\$$name is passed by reference\n";
 }
 if ( $arg->isdefaultvalueavailable() ) {
  $def = $arg->getdefaultvalue();
  $details .= "\$$name has default: $def\n";
 }
 if ( $arg->allowsnull() ) {
  $details .= "\$$name can be null\n";
 }
 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
以上就是php面向对象反射api实例详解的详细内容。
   
 
   