Pro PHP- Patterns, Frameworks, Testing and More

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

In Listing 7-8, you may replace the print_r calls with whatever way you wish to render the
result data. This could be anything from echo calls to a full-blown template engine. In any case,
the data is there, and it’s loaded from plug-ins.
Currently, you need to include each plug-in file you wish to load. In Chapter 9, which
introduces the Standard PHP Library (SPL), you will learn how to use class autoloading, so you
no longer need to include your plug-ins manually. The result will be an extremely easy-to-manage,
upload-and-go plug-in system.

Parsing Reflection-Based Documentation Data


In the previous chapter, I explained how doccomments are stored along with the program and
promised to tell you how to access that data using reflection. Accessing the doccomment data
is the easy part, and we’ll get to that, but once you have the comment data, you will need to be
able to parse it into a usable format. So, let’s first look at how comments can be parsed.

Installing the Docblock Tokenizer.


You can parse docblock comments by using the docblock tokenizer. However, installing the
tokenizer can be tricky.
The instructions here assume you have a working pecl command and are using a Unix,
Linux, or Mac OS X system. The installation should, however, work similarly on Windows systems.
Start by installing the docblock pecl extension. Execute the following command from an
administrative account:

pecl install docblock

By the time this book is published, a stable version of the docblock package should be
available. However, if it isn’t, you will see a warning like the following:

Failed to download pecl/docblock within preferred state "stable", latest release
is version 0.2.0, stability "alpha", use "channel://pecl.php.net/docblock-0.2.0"
to install

In this case, use the full-channel URL the command returns to you, and rerun the install
command in this form:

pecl install channel://pecl.php.net/docblock-0.2.0

If all went well, you now have the docblock tokenizer installed. You can confirm it is
functioning correctly with the following command:

php -i |grep DocBlock

You should then see the following result (if it is functioning correctly):

DocBlock Tokenizer support => enabled

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

Free download pdf