xref: /PHP-Parser/UPGRADE-1.0.md (revision 7503356e)
1Upgrading from PHP-Parser 0.9 to 1.0
2====================================
3
4### PHP version requirements
5
6PHP-Parser now requires PHP 5.3 or newer to run. It is however still possible to *parse* PHP 5.2 source code, while
7running on a newer version.
8
9### Move to namespaced names
10
11The library has been moved to use namespaces with the `PhpParser` vendor prefix. However, the old names using
12underscores are still available as aliases, as such most code should continue running on the new version without
13further changes.
14
15Old (still works, but discouraged):
16
17```php
18$parser = new \PHPParser_Parser(new \PHPParser_Lexer_Emulative);
19$prettyPrinter = new \PHPParser_PrettyPrinter_Default;
20```
21
22New:
23
24```php
25$parser = new \PhpParser\Parser(new PhpParser\Lexer\Emulative);
26$prettyPrinter = new \PhpParser\PrettyPrinter\Standard;
27```
28
29Note that the `PHPParser` prefix was changed to `PhpParser`. While PHP class names are technically case-insensitive,
30the autoloader will not be able to load `PHPParser\Parser` or other case variants.
31
32Due to conflicts with reserved keywords, some class names now end with an underscore, e.g. `PHPParser_Node_Stmt_Class`
33is now `PhpParser\Node\Stmt\Class_`. (But as usual, the old name is still available.)
34
35### Changes to `Node::getType()`
36
37The `Node::getType()` method continues to return names using underscores instead of namespace separators and also does
38not contain the trailing underscore that may be present in the class name. As such its output will not change in many
39cases.
40
41However, some node classes have been moved to a different namespace or renamed, which will result in a different
42`Node::getType()` output:
43
44```
45Expr_AssignBitwiseAnd => Expr_AssignOp_BitwiseAnd
46Expr_AssignBitwiseOr  => Expr_AssignOp_BitwiseOr
47Expr_AssignBitwiseXor => Expr_AssignOp_BitwiseXor
48Expr_AssignConcat     => Expr_AssignOp_Concat
49Expr_AssignDiv        => Expr_AssignOp_Div
50Expr_AssignMinus      => Expr_AssignOp_Minus
51Expr_AssignMod        => Expr_AssignOp_Mod
52Expr_AssignMul        => Expr_AssignOp_Mul
53Expr_AssignPlus       => Expr_AssignOp_Plus
54Expr_AssignShiftLeft  => Expr_AssignOp_ShiftLeft
55Expr_AssignShiftRight => Expr_AssignOp_ShiftRight
56
57Expr_BitwiseAnd       => Expr_BinaryOp_BitwiseAnd
58Expr_BitwiseOr        => Expr_BinaryOp_BitwiseOr
59Expr_BitwiseXor       => Expr_BinaryOp_BitwiseXor
60Expr_BooleanAnd       => Expr_BinaryOp_BooleanAnd
61Expr_BooleanOr        => Expr_BinaryOp_BooleanOr
62Expr_Concat           => Expr_BinaryOp_Concat
63Expr_Div              => Expr_BinaryOp_Div
64Expr_Equal            => Expr_BinaryOp_Equal
65Expr_Greater          => Expr_BinaryOp_Greater
66Expr_GreaterOrEqual   => Expr_BinaryOp_GreaterOrEqual
67Expr_Identical        => Expr_BinaryOp_Identical
68Expr_LogicalAnd       => Expr_BinaryOp_LogicalAnd
69Expr_LogicalOr        => Expr_BinaryOp_LogicalOr
70Expr_LogicalXor       => Expr_BinaryOp_LogicalXor
71Expr_Minus            => Expr_BinaryOp_Minus
72Expr_Mod              => Expr_BinaryOp_Mod
73Expr_Mul              => Expr_BinaryOp_Mul
74Expr_NotEqual         => Expr_BinaryOp_NotEqual
75Expr_NotIdentical     => Expr_BinaryOp_NotIdentical
76Expr_Plus             => Expr_BinaryOp_Plus
77Expr_ShiftLeft        => Expr_BinaryOp_ShiftLeft
78Expr_ShiftRight       => Expr_BinaryOp_ShiftRight
79Expr_Smaller          => Expr_BinaryOp_Smaller
80Expr_SmallerOrEqual   => Expr_BinaryOp_SmallerOrEqual
81
82Scalar_ClassConst     => Scalar_MagicConst_Class
83Scalar_DirConst       => Scalar_MagicConst_Dir
84Scalar_FileConst      => Scalar_MagicConst_File
85Scalar_FuncConst      => Scalar_MagicConst_Function
86Scalar_LineConst      => Scalar_MagicConst_Line
87Scalar_MethodConst    => Scalar_MagicConst_Method
88Scalar_NSConst        => Scalar_MagicConst_Namespace
89Scalar_TraitConst     => Scalar_MagicConst_Trait
90```
91
92These changes may affect custom pretty printers and code comparing the return value of `Node::getType()` to specific
93strings.
94
95### Miscellaneous
96
97  * The classes `Template` and `TemplateLoader` have been removed. You should use some other [code generation][code_gen]
98    project built on top of PHP-Parser instead.
99
100  * The `PrettyPrinterAbstract::pStmts()` method now emits a leading newline if the statement list is not empty.
101    Custom pretty printers should remove the explicit newline before `pStmts()` calls.
102
103    Old:
104
105    ```php
106    public function pStmt_Trait(PHPParser_Node_Stmt_Trait $node) {
107        return 'trait ' . $node->name
108             . "\n" . '{' . "\n" . $this->pStmts($node->stmts) . "\n" . '}';
109    }
110    ```
111
112    New:
113
114    ```php
115    public function pStmt_Trait(Stmt\Trait_ $node) {
116        return 'trait ' . $node->name
117             . "\n" . '{' . $this->pStmts($node->stmts) . "\n" . '}';
118    }
119    ```
120
121  [code_gen]: https://github.com/nikic/PHP-Parser/wiki/Projects-using-the-PHP-Parser#code-generation