Pro PHP- Patterns, Frameworks, Testing and More

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

When executed, the code in Listing 7-9 results in the following:

/**

* This is a doccomment
*
* This doccomment documents the class demo
*/

Next, let’s tokenize this data.

Tokenizing Doccomment Data


The docblock tokenizer pecl extension’s primary method is mixed docblock_tokenize($comment,
$terseMode=false). The terse mode parameter controls whether nonsemantic tokens like the
commentstart token are included in parsing. The function returns an array of tokens and their
associated data.
The second function you will need is docblock_token_name($token). This function takes a
token identifier, which is the first index of each array entry that is returned from tokenization.
The function transforms the numeric token ID into a human-readable string. Listing 7-10
demonstrates the tokenization process.

Listing 7-10. Tokenizing Doccomments

/**
* This is a doccomment
*
* This doccomment documents the class demo
*/
class demo {}

$reflectionClass = new ReflectionClass('demo');
$docComment = $reflectionClass->getDocComment();

$tokens = docblock_tokenize($docComment,true);

foreach($tokens as $token) {
echo $token[0]. '=';
echo docblock_token_name($token[0]). '=';
print_r($token[1]);
echo "\n";
}

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

Free download pdf