1 Constant expression evaluation
2 ==============================
4 Initializers for constants, properties, parameters, etc. have limited support for expressions. For
10 const SECONDS_IN_HOUR = 60 * 60;
11 const SECONDS_IN_DAY = 24 * self::SECONDS_IN_HOUR;
15 PHP-Parser supports evaluation of such constant expressions through the `ConstExprEvaluator` class:
20 use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
22 $evalutator = new ConstExprEvaluator();
24 $value = $evalutator->evaluateSilently($someExpr);
25 } catch (ConstExprEvaluationException $e) {
26 // Either the expression contains unsupported expression types,
27 // or an error occurred during evaluation
34 The constant evaluator provides two methods, `evaluateDirectly()` and `evaluateSilently()`, which
35 differ in error behavior. `evaluateDirectly()` will evaluate the expression as PHP would, including
36 any generated warnings or Errors. `evaluateSilently()` will instead convert warnings and Errors into
37 a `ConstExprEvaluationException`. For example:
42 use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
43 use PhpParser\Node\{Expr, Scalar};
45 $evaluator = new ConstExprEvaluator();
48 $expr = new Expr\BinaryOp\Div(new Scalar\LNumber(10), new Scalar\LNumber(0));
50 var_dump($evaluator->evaluateDirectly($expr)); // float(INF)
51 // Warning: Division by zero
54 $evaluator->evaluateSilently($expr);
55 } catch (ConstExprEvaluationException $e) {
56 var_dump($e->getPrevious()->getMessage()); // Division by zero
60 For the purposes of static analysis, you will likely want to use `evaluateSilently()` and leave
61 erroring expressions unevaluated.
63 Unsupported expressions and evaluator fallback
64 ----------------------------------------------
66 The constant expression evaluator supports all expression types that are permitted in constant
67 expressions, apart from the following:
69 * `Scalar\MagicConst\*`
70 * `Expr\ConstFetch` (only null/false/true are handled)
71 * `Expr\ClassConstFetch`
73 Handling these expression types requires non-local information, such as which global constants are
74 defined. By default, the evaluator will throw a `ConstExprEvaluationException` when it encounters
75 an unsupported expression type.
77 It is possible to override this behavior and support resolution for these expression types by
78 specifying an evaluation fallback function:
83 use PhpParser\{ConstExprEvaluator, ConstExprEvaluationException};
84 use PhpParser\Node\Expr;
86 $evalutator = new ConstExprEvaluator(function(Expr $expr) {
87 if ($expr instanceof Expr\ConstFetch) {
88 return fetchConstantSomehow($expr);
90 if ($expr instanceof Expr\ClassConstFetch) {
91 return fetchClassConstantSomehow($expr);
94 throw new ConstExprEvaluationException(
95 "Expression of type {$expr->getType()} cannot be evaluated");
99 $evalutator->evaluateSilently($someExpr);
100 } catch (ConstExprEvaluationException $e) {
105 Implementers are advised to ensure that evaluation of indirect constant references cannot lead to
106 infinite recursion. For example, the following code could lead to infinite recursion if constant
107 lookup is implemented naively.