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
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/php-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
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
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('<?php ...', $version=120);
// or
$ast = ast\parse_file('file.php', $version=120);
The abstract syntax tree returned by these functions consists of ast\Node objects.
ast\Node is declared as follows:
namespace ast;
class Node {
public $kind;
public $flags;
public $lineno;
public $children;
}
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:
<?php
$code = <<<'EOC'
<?php
$var = 42;
EOC;
var_dump(ast\parse_code($code, $version=70));
// Output:
object(ast\Node)#1 (4) {
["kind"]=>
int(132)
["flags"]=>
int(0)
["lineno"]=>
int(1)
["children"]=>
array(1) {
[0]=>
object(ast\Node)#2 (4) {
["kind"]=>
int(517)
["flags"]=>
int(0)
["lineno"]=>
int(2)
["children"]=>
array(2) {
["var"]=>
object(ast\Node)#3 (4) {
["kind"]=>
int(256)
["flags"]=>
int(0)
["lineno"]=>
int(2)
["children"]=>
array(1) {
["name"]=>
string(3) "var"
}
}
["expr"]=>
int(42)
}
}
}
}
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:
<?php
require 'path/to/util.php';
$code = <<<'EOC'
<?php
$var = 42;
EOC;
echo ast_dump(ast\parse_code($code, $version=70)), "\n";
// Output:
AST_STMT_LIST
0: AST_ASSIGN
var: AST_VAR
name: "var"
expr: 42
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.
namespace ast;
class Metadata
{
public $kind;
public $name;
public $flags;
public $flagsCombinable;
}
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.
// Used by ast\AST_NAME (exclusive)
ast\flags\NAME_FQ (= 0) // example: \Foo\Bar
ast\flags\NAME_NOT_FQ // example: Foo\Bar
ast\flags\NAME_RELATIVE // example: namespace\Foo\Bar
// Used by ast\AST_METHOD, ast\AST_PROP_DECL, ast\AST_PROP_GROUP,
// ast\AST_CLASS_CONST_DECL, ast\AST_CLASS_CONST_GROUP, and ast\AST_TRAIT_ALIAS (combinable)
ast\flags\MODIFIER_PUBLIC
ast\flags\MODIFIER_PROTECTED
ast\flags\MODIFIER_PRIVATE
ast\flags\MODIFIER_STATIC
ast\flags\MODIFIER_ABSTRACT
ast\flags\MODIFIER_FINAL
ast\flags\MODIFIER_READONLY
ast\flags\MODIFIER_PUBLIC_SET
ast\flags\MODIFIER_PROTECTED_SET
ast\flags\MODIFIER_PRIVATE_SET
// Used by ast\AST_CLOSURE, ast\AST_ARROW_FUNC (combinable)
ast\flags\MODIFIER_STATIC
// Used by ast\AST_FUNC_DECL, ast\AST_METHOD, ast\AST_CLOSURE, ast\AST_ARROW_FUNC (combinable)
ast\flags\FUNC_RETURNS_REF // legacy alias: ast\flags\RETURNS_REF
ast\flags\FUNC_GENERATOR // used only in PHP >= 7.1
// Used by ast\AST_CLOSURE_VAR
ast\flags\CLOSURE_USE_REF
// Used by ast\AST_CLASS (combinable since PHP 8.1 enums)
ast\flags\CLASS_ABSTRACT
ast\flags\CLASS_FINAL
ast\flags\CLASS_TRAIT
ast\flags\CLASS_INTERFACE
ast\flags\CLASS_ANONYMOUS
ast\flags\CLASS_ENUM // php 8.1 enums
ast\flags\CLASS_READONLY // php 8.2 readonly classes
// Used by ast\AST_PARAM (combinable)
ast\flags\PARAM_REF
ast\flags\PARAM_VARIADIC
ast\flags\PARAM_MODIFIER_PUBLIC (available since 1.0.8, same as ast\flags\MODIFIER_* in PHP >= 8.0)
ast\flags\PARAM_MODIFIER_PROTECTED (available since 1.0.8)
ast\flags\PARAM_MODIFIER_PRIVATE (available since 1.0.8)
// Used by ast\AST_TYPE (exclusive)
ast\flags\TYPE_ARRAY
ast\flags\TYPE_CALLABLE
ast\flags\TYPE_VOID
ast\flags\TYPE_BOOL
ast\flags\TYPE_LONG
ast\flags\TYPE_DOUBLE
ast\flags\TYPE_STRING
ast\flags\TYPE_ITERABLE
ast\flags\TYPE_OBJECT
ast\flags\TYPE_NULL // php 8.0 union types
ast\flags\TYPE_FALSE // php 8.0 union types
ast\flags\TYPE_TRUE // php 8.2 true type
ast\flags\TYPE_STATIC // php 8.0 static return type
ast\flags\TYPE_MIXED // php 8.0 mixed type
ast\flags\TYPE_NEVER // php 8.1 never type
// Used by ast\AST_CAST (exclusive)
ast\flags\TYPE_NULL
ast\flags\TYPE_BOOL
ast\flags\TYPE_LONG
ast\flags\TYPE_DOUBLE
ast\flags\TYPE_STRING
ast\flags\TYPE_ARRAY
ast\flags\TYPE_OBJECT
// Used by ast\AST_UNARY_OP (exclusive)
ast\flags\UNARY_BOOL_NOT
ast\flags\UNARY_BITWISE_NOT
ast\flags\UNARY_MINUS
ast\flags\UNARY_PLUS
ast\flags\UNARY_SILENCE
// Used by ast\AST_BINARY_OP and ast\AST_ASSIGN_OP (exclusive)
ast\flags\BINARY_BITWISE_OR
ast\flags\BINARY_BITWISE_AND
ast\flags\BINARY_BITWISE_XOR
ast\flags\BINARY_CONCAT
ast\flags\BINARY_ADD
ast\flags\BINARY_SUB
ast\flags\BINARY_MUL
ast\flags\BINARY_DIV
ast\flags\BINARY_MOD
ast\flags\BINARY_POW
ast\flags\BINARY_SHIFT_LEFT
ast\flags\BINARY_SHIFT_RIGHT
ast\flags\BINARY_COALESCE
ast\flags\BINARY_PIPE
// Used by ast\AST_BINARY_OP (exclusive)
ast\flags\BINARY_BOOL_AND
ast\flags\BINARY_BOOL_OR
ast\flags\BINARY_BOOL_XOR
ast\flags\BINARY_IS_IDENTICAL
ast\flags\BINARY_IS_NOT_IDENTICAL
ast\flags\BINARY_IS_EQUAL
ast\flags\BINARY_IS_NOT_EQUAL
ast\flags\BINARY_IS_SMALLER
ast\flags\BINARY_IS_SMALLER_OR_EQUAL
ast\flags\BINARY_IS_GREATER
ast\flags\BINARY_IS_GREATER_OR_EQUAL
ast\flags\BINARY_SPACESHIP
// Used by ast\AST_MAGIC_CONST (exclusive)
ast\flags\MAGIC_LINE
ast\flags\MAGIC_FILE
ast\flags\MAGIC_DIR
ast\flags\MAGIC_NAMESPACE
ast\flags\MAGIC_FUNCTION
ast\flags\MAGIC_METHOD
ast\flags\MAGIC_CLASS
ast\flags\MAGIC_TRAIT
// Used by ast\AST_USE, ast\AST_GROUP_USE and ast\AST_USE_ELEM (exclusive)
ast\flags\USE_NORMAL
ast\flags\USE_FUNCTION
ast\flags\USE_CONST
// Used by ast\AST_INCLUDE_OR_EVAL (exclusive)
ast\flags\EXEC_EVAL
ast\flags\EXEC_INCLUDE
ast\flags\EXEC_INCLUDE_ONCE
ast\flags\EXEC_REQUIRE
ast\flags\EXEC_REQUIRE_ONCE
// Used by ast\AST_ARRAY (exclusive), since PHP 7.1
ast\flags\ARRAY_SYNTAX_SHORT
ast\flags\ARRAY_SYNTAX_LONG
ast\flags\ARRAY_SYNTAX_LIST
// Used by ast\AST_ARRAY_ELEM (exclusive)
ast\flags\ARRAY_ELEM_REF
// Used by ast\AST_DIM (combinable), since PHP 7.4
ast\flags\DIM_ALTERNATIVE_SYNTAX
ast\flags\ENCAPS_VAR_DOLLAR_CURLY // php 8.2 deprecation, only available in php 8.2+
// Used by ast\AST_VAR (combinable), since PHP 8.2
ast\flags\ENCAPS_VAR_DOLLAR_CURLY
ast\flags\ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR
// Used by ast\AST_CONDITIONAL (combinable), since PHP 7.4
ast\flags\PARENTHESIZED_CONDITIONAL
AST node kinds
This section lists the AST node kinds that are supported and the names of their child nodes.