# PHP-Parser

> A PHP parser written in PHP

- Type: agent or tool
- Field: Coding & Development
- Repository: https://github.com/nikic/PHP-Parser
- GitHub stars: 17485
- Licence: BSD-3-Clause
- Language: PHP
- Last push: 2026-11-22
- Page: https://imutechz.in/agents/php-parser

## What it is
PHP-Parser is a PHP parser written in PHP, intended to simplify static code analysis and manipulation. It parses PHP 7 and PHP 8 code into an abstract syntax tree with accurate location information, and can convert an AST back to PHP code. It is aimed at developers building tools that analyze or transform PHP source.

## Features
- Parses PHP 7 and PHP 8 code into an AST, with partial ASTs for invalid code
- Dumps the AST in human-readable form
- Converts an AST back to PHP code, preserving formatting for partially changed ASTs
- Infrastructure to traverse and modify ASTs
- Name resolution for namespaced names and constant expression evaluation
- AST builders and JSON encoding/decoding of ASTs

## Best for
- Static analysis tools for PHP code
- Automated code refactoring and transformation
- Code generation from constructed ASTs

## Pros
- Provides accurate location information in the AST
- Can parse invalid code into a partial AST
- Formatting can be preserved for partially changed ASTs

## Cons
- Version 5.x requires PHP = 7.4 to run
- Version 5.x has only limited support for parsing PHP 5.x

## Install
```
php composer.phar require nikic/php-parser
```

## Usage
```
$parser = (new ParserFactory())->createForNewestSupportedVersion();
```

Tags: php, parser, ast, static-analysis, code-transformation, pretty-printing, composer
