PHP Parser
This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and manipulation.
Documentation for version 5.x [in development; for running on PHP >= 7.1; for parsing PHP 7.0 to PHP 8.2, with limited support for parsing PHP 5.x].
Documentation for version 4.x [stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.2].
Documentation for version 3.x [unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2].
Features
The main features provided by this library are:
- Parsing PHP 7, and PHP 8 code into an abstract syntax tree [AST].
- Invalid code can be parsed into a partial AST.
- The AST contains accurate location information.
- Dumping the AST in human-readable form.
- Converting an AST back to PHP code.
- Formatting can be preserved for partially changed ASTs.
- Infrastructure to traverse and modify ASTs.
- Resolution of namespaced names.
- Evaluation of constant expressions.
- Builders to simplify AST construction for code generation.
- Converting an AST into JSON and back.
Quick Start
Install the library using composer:
php composer.phar require nikic/php-parser
Parse some PHP code into an AST and dump the result in human-readable form: