PHPackages                             nikic/php-ast - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. nikic/php-ast

ActivePhp-ext

nikic/php-ast
=============

Extension exposing PHP 7 abstract syntax tree

v1.1.3(9mo ago)9775.8k↓42.3%82[5 PRs](https://github.com/nikic/php-ast/pulls)BSD-3-ClausePHPPHP &gt;= 7.2.0CI passing

Since Aug 10Pushed 5mo ago28 watchersCompare

[ Source](https://github.com/nikic/php-ast)[ Packagist](https://packagist.org/packages/nikic/php-ast)[ RSS](/packages/nikic-php-ast/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)DependenciesVersions (2)Used By (0)

php-ast
=======

[](#php-ast)

This extension exposes the abstract syntax tree generated by PHP 7 and 8.

**This is the documentation for version 1.x.y. See also [documentation for version 0.1.x](https://github.com/nikic/php-ast/tree/v0.1.x#php-ast).**

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [API overview](#api-overview)
- [Basic usage](#basic-usage)
- [Example](#example)
- [Metadata](#metadata)
- [Flags](#flags)
- [AST node kinds](#ast-node-kinds)
- [AST versioning](#ast-versioning)
- [Differences to PHP-Parser](#differences-to-php-parser)

Installation
------------

[](#installation)

**Windows**: Download a [prebuilt Windows DLL](https://downloads.php.net/~windows/pecl/releases/ast/)and move it into the `ext/` directory of your PHP installation. Furthermore, add `extension=php_ast.dll` to your `php.ini` file. (The [php-ast PECL page](https://downloads.php.net/~windows/pecl/releases/ast/) also links to the same DLLs for Windows.)

**Unix (PECL)**: Run `pecl install ast` and add `extension=ast.so` to your `php.ini`.

**Unix (PIE)**: Run `pie install nikic/ast` and add `extension=ast.so` to your `php.ini`.

**Unix (Compile)**: Compile and install the extension as follows.

```
phpize
./configure
make
sudo make install
```

Additionally add `extension=ast.so` to your `php.ini` file.

API overview
------------

[](#api-overview)

Defines:

- `ast\Node` class
- `ast\Metadata` class
- `ast\AST_*` kind constants
- `ast\flags\*` flag constants
- `ast\parse_file(string $filename, int $version)`
- `ast\parse_code(string $code, int $version [, string $filename = "string code"])`
- `ast\get_kind_name(int $kind)`
- `ast\kind_uses_flags(int $kind)`
- `ast\get_metadata()`
- `ast\get_supported_versions(bool $exclude_deprecated = false)`

Basic usage
-----------

[](#basic-usage)

Code can be parsed using either `ast\parse_code()`, which accepts a code string, or `ast\parse_file()`, which accepts a file path. Additionally, both functions require a `$version`argument to ensure forward-compatibility. The current version is 120.

```
$ast = ast\parse_code('
