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