The tokenizer functions are quite powerful. For example, you can retrieve all of the methods in a given class using an algorithm like:
for each token:
if token is T_FUNCTION then start buffer
if buffer is started then add the current string to the buffer
if token is ( stop buffer
And the great thing is that the class methods will have the right case, so it's a good way to get around the limitations with get_class_methods returning lowercase method names. Also since using a similar algorithm you can read the arguments of a function you can implement Reflections-like functionality into PHP4.
Finally you can use it as a simpler method of extracting Javadoc out of a class file to generate documentation. The util/MethodTable.php class in AMFPHP (http://www.amfphp.org) uses the tokenizer functions to create a method table with all of the arguments, a description, return type, etc. and from that method table it can generate ActionScript that matches the PHP, but it could also be fitted to generate JavaScript, documentation files, or basically anything you put your mind to. I can also see that this could be the base for a class -> WSDL file generator.
CLIX. Funções de Tokenizer
Introdução
As funções de tokenizer formam uma interface para o tokenizer do PHP imbutido no Engine Zend. Usando essas funções você pode escrever ferramentas próprias para análise e modificação de código PHP sem ter que lidar com a especificação da linguagem em um nível léxico.
Veja também o apêndice sobre tokens.
Dependências
Nenhuma biblioteca externa é necessária para compilar esta extensão.
Instalação
Começando no PHP 4.3.0, essas funções estão habilitadas a menos que seja dito o contrário. Para versões anteriores, você tem que configurar e compilar o PHP com --enable-tokenizer. Você pode desabilitar suporte à extensão tokenizer com --disable-tokenizer.
A versão para Windows do PHP tem suporte embutido para esta extensão. Você não precisa carregar nenhuma extensão adicional para utilizar essas funções.
Nota: Suporte integrado para tokenizer está disponível a partir do PHP 4.3.0.
Constantes pré-definidas
Quando a extensão for ou compilado no PHP ou carregada dinamicamente em tempo de execução, os tokens listados em Apêndice Q são definidos como constantes.
Exemplos
Aqui está um exemplo simples de scripts PHP usando o tokenizer que lerá um arquivo PHP, retirará todos os comentários do fonte e imprimirá apenas o código puro.
- Índice
- token_get_all -- Divide um dado fonte em tokens do PHP
- token_name -- Pega o nome simbólico de um dado token do PHP
