PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 5 ■ OBJECT TOOLS


Class Description

ReflectionParameter Method argument information

ReflectionProperty Class property information

ReflectionFunction Function information and tools

ReflectionExtension
PHP extension information

ReflectionException An error class

Time to Roll Up Your Sleeves


You have already encountered some functions for examining the attributes of classes. These are useful
but often limited. Here’s a tool that is up to the job. ReflectionClass provides methods that reveal
information about every aspect of a given class, whether it’s a user-defined or internal class. The
constructor of ReflectionClass accepts a class name as its sole argument:


$prod_class = new ReflectionClass( 'CdProduct' );
Reflection::export( $prod_class );


Once you’ve created a ReflectionClass object, you can use the Reflection utility class to dump
information about CdProduct. Reflection has a static export() method that formats and dumps the data
managed by a Reflection object (that is, any instance of a class that implements the Reflector interface,
to be pedantic). Here’s an slightly amended extract from the output generated by a call to
Reflection::export():


Class [ class CdProduct extends ShopProduct ] {
@@ fullshop.php 53-73



  • Constants [0] {
    }

  • Static properties [0] {
    }

  • Static methods [0] {
    }

  • Properties [2] {
    Property [ private $playLength ]
    Property [ protected $price ]
    }

  • Methods [10] {
    Method [ <user, overwrites ShopProduct, ctor> public method __construct ] {
    @@ fullshop.php 56 - 61

  • Parameters [5] {
    Parameter #0 [ $title ]

Free download pdf