PHP Object-oriented Reflection API example

Source: Internet
Author: User
Reflection API

fullshop.php

<?phpclass 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;  The 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 ($titl    E, $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; }}/* $product 1 = new Cdproduct ("CD1", "Bob", "Bobbleson", 4,);p rint $product 1->getsummaryline (). " \ n "; $product 2 = new Bookproduct (" Book1 "," Harry "," Harrelson ", 4,);p rint $product 2->getsummaryline ()." \ n "; */?><?phprequire_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 $play Length] Property [<default> protected $price]}-Methods [ten] {Method [<user, overwrites Shopproduct, CTO R> 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> $mainNam E] Parameter #3 [<required> $price] Parameter #4 [<optional> $playLength = +]}} Method [&lt ;user> Public method Getplaylength] {@@ d:\xampp\htdocs\popp-code\5\fullshop.php 63-65} method [<user, Ove Rwrites 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 1 7-19} method [<user, Inherits Shopproduct> public Method Getproducermainname] {@@ D:\XAMPP\HTDOCS\POPP-COD E\5\fullshop.php 21-23} method [<user, Inherits Shopproduct> public Method SetDiscount] {@@ D:\xampp\htdoc s\popp-code\5\fullshop.php 25-27-parameters [1] {Parameter #0 [<required> $num]}} Method [<use  R, 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- The method [<user, Inherits shopproduct> public method Getproducer] {@@ d:\xampp\htdocs\popp-code\5\fullshop . PHP 41-44}}}

Comments: The class to see a thorough mess, more than the var_dump stronger. Which properties inherit what class. The methods in the class are self-contained, which are rewritten, and which are inherited, at a glance.

View class data

<?phprequire_once ("fullshop.php"), function Classdata (Reflectionclass $class) {$details = ""; $name = $class->getname (); if ($class->isuserdefined ()) {  $details. = "$name is User defined\n";} if ($class->isinternal ()) {  $det ails. = "$name is built-in\n"; if ($class->isinterface ()) {  $details. = "$name is interface\n";} if ($class->isabstract ()) {  $detail S. = "$name is an abstract class\n"; if ($class->isfinal ()) {  $details. = "$name is a final class\n";} if ($class->isinstantiable ()) {  $de Tails. = "$name can be instantiated\n"; } else {  $details. = "$name can not be instantiated\n";} return $details;} $prod _class = new Reflectionclass (' cdproduct ');p rint classdata ($prod _class);? >

Output

Cdproduct is User defined
Cdproduct can instantiated

View Method Data

<?phprequire_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. = "$n Ame 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 I S 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 definedconstruct are Publicconstruct is the constructor----getplaylength are user definedgetplaylength is Public----Getsummaryline are user definedgetsummaryline is public----getproducerfirstname is user Definedgetproducerfirstname is public----getproducermainname are user definedgetproducermainname is Public----SetDiscount are user definedsetdiscount are public----getdiscount are user definedgetdiscount is Public----GetTitle are user definedgettitle are public----getprice are user definedgetprice are public----getproducer are user Definedgetproducer is public

Get constructor parameter condition

<?phprequire_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->isdefaultval Ueavailable ()) {  $def = $arg->getdefaultvalue ();  $details. = "\$ $name has default: $def \ n"; if ($arg->allowsnull ()) {  $details. = "\$ $name can be null\n";} return $details;}? >

Output

 $title have position 0$title can be null$firstname have position 1$firstname can be nul  L$mainname have position 2$mainname can be null$price have position 3$price can be null$playlength have position 4$playlength Has default:78$playlength can is null 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.