Skip to content

Commit fec4dd6

Browse files
committed
Add "Differences to PHP-Parser" section
1 parent f199ac8 commit fec4dd6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,52 @@ Supported since 2015-10-21. Deprecated since 2016-04-30.
462462

463463
Initial.
464464

465+
Differences to PHP-Parser
466+
-------------------------
467+
468+
Next to php-ast I also maintain the [PHP-Parser][php-parser] library, which has some overlap with
469+
this extension. This section summarizes the main differences between php-ast and PHP-Parser so you
470+
may decide which is preferable for your use-case.
471+
472+
The primary difference is that php-ast is a PHP extension (written in C) which exports the AST
473+
internally used by PHP 7. PHP-Parser on the other hand is library written in PHP. This has a number
474+
of consequences:
475+
476+
* php-ast is significantly faster than PHP-Parser, because the AST construction is implemented in
477+
C.
478+
* php-ast needs to be installed as an extension, on Linux either by compiling it manually or
479+
retrieving it from a package manager, on Windows by loading a DLL. PHP-Parser is installed as a
480+
Composer dependency.
481+
* php-ast only runs on PHP >= 7.0, as prior versions did not use an internal AST. PHP-Parser
482+
supports PHP >= 5.4.
483+
* php-ast may only parse code that is syntactically valid on the version of PHP it runs on. This
484+
means that it's not possible to parse code using features of newer versions (e.g. PHP 7.1 code
485+
while running on PHP 7.0). Similarly, it is not possible to parse code that is no longer
486+
syntactically valid on the used version (e.g. some PHP 5 code may no longer be parsed -- however
487+
most code will work). PHP-Parser supports parsing both newer and older (up to PHP 5.2) versions.
488+
489+
There are a number of differences in the AST representation and available support code:
490+
491+
* The PHP-Parser library uses a separate class for every node type, with child nodes stored as
492+
type-annotated properties. php-ast uses one class for everything, with children stored as
493+
arrays. The former approach is friendlier to developers because it has very good IDE integration.
494+
The php-ast extension does not use separate classes, because registering hundreds of classes was
495+
judged a no-go for a bundled extension.
496+
* The PHP-Parser library contains various support code for working with the AST, while php-ast only
497+
handles AST construction. The reason for this is that implementing this support code in C is
498+
extremely complicated and there is little other benefit to implementing it in C. The main
499+
components that PHP-Parser offers that may be of interest are:
500+
* Node dumper (human readable representation): While the php-ast extension does not directly
501+
implement this, a `ast_dump` function is provided in the `util.php` file.
502+
* Pretty printer (converting the AST back to PHP code): This is not provided natively, but the
503+
[php-ast-reverter][php-ast-reverter] package implements this functionality.
504+
* Name resolution (resolving namespace prefixes and aliases): There is currently no standalone
505+
package for this.
506+
* AST traversation / visitation: There is currently no standalone package for this either, but
507+
implementing a recursive AST walk is easy.
508+
465509
[parser]: http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_language_parser.y
466510
[util]: https://github.com/nikic/php-ast/blob/master/util.php
467511
[test_dump]: https://github.com/nikic/php-ast/blob/master/tests/001.phpt
512+
[php-parser]: https://github.com/nikic/PHP-Parser
513+
[ast-reverter]: https://github.com/tpunt/php-ast-reverter

0 commit comments

Comments
 (0)