Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 7 ■ REFLECTION API^75

foreach(get_declared_classes() as $class) {
$reflectionClass = new ReflectionClass($class);
if($reflectionClass->isUserDefined()) {
Reflection::export($reflectionClass);
}
}

When executed, the code in Listing 7-1 will show something like the following:

Class [ <user> class userClass ] {
@@ reflector.php 3-5


  • Constants [0] {
    }

  • Static properties [0] {
    }

  • Static methods [0] {
    }

  • Properties [0] {
    }

  • Methods [1] {
    Method [ public method userMethod ] {
    @@ reflector.php 4 - 4

  • Parameters [1] {
    Parameter #0 [ $userParameter = 'default' ]
    }
    }
    }
    }


In Listing 7-1, you start actually using program metadata to make decisions. You learned
about metadata in the previous chapter. Metadata can be further split into two different types:


  • Hard metadata is metadata that is exposed by parsed code. It includes metadata like
    class names, methods, parameters, and so on.

  • Soft metadata is any human-included data like a PHPDoc block, and in PHP, attributes.


While hard metadata is parsed by PHP and is guaranteed to be valid, soft metadata is prone
to human error, and its structure is not governed by the PHP lexing process. Here’s an example:

McArthur_819-9C07.fm Page 75 Friday, February 22, 2008 8:59 AM

Free download pdf