Download the PHP package nikic/php-ast without Composer
On this page you can find all versions of the php package nikic/php-ast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nikic/php-ast
More information about nikic/php-ast
Files in nikic/php-ast
Package php-ast
Short Description Extension exposing PHP 7 abstract syntax tree
License BSD-3-Clause
Informations about the package 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.
Table of Contents
- Installation
- API overview
- Basic usage
- Example
- Metadata
- Flags
- AST node kinds
- AST versioning
- Differences to PHP-Parser
Installation
Windows: Download a prebuilt Windows DLL
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 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.
Additionally add extension=ast.so
to your php.ini
file.
API overview
Defines:
ast\Node
classast\Metadata
classast\AST_*
kind constantsast\flags\*
flag constantsast\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
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.
The abstract syntax tree returned by these functions consists of ast\Node
objects.
ast\Node
is declared as follows:
The kind
property specifies the type of the node. It is an integral value, which corresponds to
one of the ast\AST_*
constants, for example ast\AST_STMT_LIST
. See the
AST node kinds section for an overview of the available node kinds.
The flags
property contains node specific flags. It is always defined, but for most nodes it is
always zero. See the flags section for a list of flags supported by the different node
kinds.
The lineno
property specifies the starting line number of the node.
The children
property contains an array of child-nodes. These children can be either other
ast\Node
objects or plain values. There are two general categories of nodes: Normal AST nodes,
which have a fixed set of named child nodes, as well as list nodes, which have a variable number
of children. The AST node kinds section contains a list of the child names for
the different node kinds.
Example
Simple usage example:
The util.php
file defines an ast_dump()
function, which can be used to create a more
compact and human-readable dump of the AST structure:
To additionally show line numbers pass the AST_DUMP_LINENOS
option as the second argument to
ast_dump()
.
A more substantial AST dump can be found in the tests.
Metadata
There are a number of functions which provide meta-information for the AST structure:
ast\get_kind_name()
returns a string name for an integral node kind.
ast\kind_uses_flags()
determines whether the flags
of a node kind may ever be non-zero.
ast\get_metadata()
returns metadata about all AST node kinds. The return value is a map from AST
node kinds to ast\Metadata
objects defined as follows.
The kind
is the integral node kind, while name
is the same name as returned by the
get_kind_name()
function.
flags
is an array of flag constant names, which may be used by the node kind. flagsCombinable
specifies whether the flag should be checked using ===
(not combinable) or using &
(combinable).
Together these two values provide programmatic access to the information listed in the
flags section.
The AST metadata is intended for use in tooling for working the AST, such as AST dumpers.
Flags
This section lists which flags are used by which AST node kinds. The "combinable" flags can be
combined using bitwise or and should be checked by using $ast->flags & ast\flags\FOO
. The
"exclusive" flags are used standalone and should be checked using $ast->flags === ast\flags\BAR
.
AST node kinds
This section lists the AST node kinds that are supported and the names of their child nodes.
AST Versioning
The ast\parse_code()
and ast\parse_file()
functions each accept a required AST $version
argument. The idea behind the AST version is that we may need to change the format of the AST to
account for new features in newer PHP versions, or to improve it in other ways. Such changes will
always be made under a new AST version, while previous formats continue to be supported for some
time.
Old AST versions may be deprecated in minor versions and removed in major versions of the AST extension.
A list of currently supported versions is available through ast\get_supported_versions()
. The
function accepts a boolean argument that determines whether deprecated versions should be excluded.
In the following the changes in the respective AST versions, as well as their current support state, are listed.
120 (current)
Supported since 1.1.3 (TBD).
clone $expr
is now represented like a function call (usingAST_CALL
instead ofAST_CLONE
).exit($expr)
is now represented like a function call (usingAST_CALL
instead ofAST_EXIT
).
110 (stable)
Supported since 1.1.2 (2024-08-10).
- Add a
hooks
child node forAST_PROP_ELEM
(PHP 8.4 property hooks) andAST_PARAM
(constructor property promotion can have property hooks) - Add new node kinds
AST_PROPERTY_HOOK
andAST_PROPERTY_HOOK_SHORT_BODY
. - Remove the
name
child node from theAST_ARROW_FUNC
andAST_CLOSURE
nodes (previously"{closure}"
)
100 (stable)
Supported since 1.1.1 (2023-11-12).
- Add a
type
child node for allAST_CLASS_CONST_GROUP
nodes.
90 (stable)
Supported since 1.0.14 (2021-07-24)
- Same as AST version 85.
85 (stable)
Supported since 1.0.11 (2021-04-20)
- Add a
type
child node (for enum type) for allAST_CLASS
nodes.
80 (stable)
Supported since 1.0.10 (2020-09-12).
mixed
type hints are now reported as anAST_TYPE
with typeTYPE_MIXED
instead of anAST_NAME
.AST_CLASS_CONST_GROUP
nodes are emitted for class constant declarations wrapping theAST_CLASS_CONST_DECL
and any attributes. Previously,AST_CLASS_CONST_DECL
would be emitted.AST_PARAM
,AST_CLASS_DECL
,AST_METHOD
,AST_PROP_DECL
,AST_CLOSURE
,AST_FUNC_DECL
, andAST_ARROW_FUNC
nodes now contain an attributes child.
70 (stable)
Supported since 1.0.1 (2019-02-11).
-
AST_PROP_GROUP
was added to support PHP 7.4's typed properties. The property visibility modifiers are now part ofAST_PROP_GROUP
instead ofAST_PROP_DECL
.Note that property group type information is only available with AST versions 70+.
AST_CLASS_NAME
is created instead ofAST_CLASS_CONST
forSomeClass::class
.
60 (deprecated)
Supported since 0.1.7 (2018-10-06). Deprecated in php-ast 1.1.0.
AST_FUNC_DECL
andAST_METHOD
no longer generate auses
child. Previously this child was alwaysnull
.AST_CONST_ELEM
now always has adocComment
child. Previously it was absent on PHP 7.0. On PHP 7.0 the value is alwaysnull
.
50 (deprecated)
Supported since 0.1.5 (2017-07-19). Deprecated in php-ast 1.1.0.
This is the oldest AST version available in 1.0.0. See the 0.1.x AST version changelog for information on changes prior to this version.
Differences to PHP-Parser
Next to php-ast I also maintain the PHP-Parser library, which has some overlap with this extension. This section summarizes the main differences between php-ast and PHP-Parser so you may decide which is preferable for your use-case.
The primary difference is that php-ast is a PHP extension (written in C) which exports the AST internally used by PHP 7 and 8. PHP-Parser on the other hand is library written in PHP. This has a number of consequences:
- php-ast is significantly faster than PHP-Parser, because the AST construction is implemented in C.
- php-ast needs to be installed as an extension, on Linux either by compiling it manually or retrieving it from a package manager, on Windows by loading a DLL. PHP-Parser is installed as a Composer dependency.
- php-ast only runs on PHP >= 7.0 (php-ast 1.0.16 was the last version supporting php <= 7.1), as prior versions did not use an internal AST. PHP-Parser supports PHP >= 5.5.
- php-ast may only parse code that is syntactically valid on the version of PHP it runs on. This means that it's not possible to parse code using features of newer versions (e.g. PHP 7.4 code while running on PHP 7.2). Similarly, it is not possible to parse code that is no longer syntactically valid on the used version (e.g. some PHP 5 code may no longer be parsed -- however most code will work). PHP-Parser supports parsing both newer and older (up to PHP 5.2) versions.
- php-ast can only parse code which is syntactically valid, while PHP-Parser can provide a partial AST for code that contains errors (e.g., because it is currently being edited).
- php-ast only provides the starting line number (and for declarations the ending line number) of nodes, because this is the only part that PHP itself stores. PHP-Parser provides precise file offsets.
There are a number of differences in the AST representation and available support code:
- The PHP-Parser library uses a separate class for every node type, with child nodes stored as type-annotated properties. php-ast uses one class for everything, with children stored as arrays. The former approach is friendlier to developers because it has very good IDE integration. The php-ast extension does not use separate classes, because registering hundreds of classes was judged a no-go for a bundled extension.
- The PHP-Parser library contains various support code for working with the AST, while php-ast only
handles AST construction. The reason for this is that implementing this support code in C is
extremely complicated and there is little other benefit to implementing it in C. The main
components that PHP-Parser offers that may be of interest are:
- Node dumper (human readable representation): While the php-ast extension does not directly
implement this, a
ast_dump
function is provided in theutil.php
file. - Pretty printer (converting the AST back to PHP code): This is not provided natively, but the php-ast-reverter package implements this functionality.
- Name resolution (resolving namespace prefixes and aliases): There is currently no standalone package for this.
- AST traversal / visitation: There is currently no standalone package for this either, but implementing a recursive AST walk is easy.
- Node dumper (human readable representation): While the php-ast extension does not directly
implement this, a
The tolerant-php-parser-to-php-ast project can convert the AST produced by tolerant-php-parser (Another pure PHP parser library) into the format used by the php-ast extension. This can be used as a slow fallback in case the php-ast extension is not available. It may also be used to produce a partial php-ast output for code with syntax errors.