ConstExprEvaluator
in package
Evaluates constant expressions.
This evaluator is able to evaluate all constant expressions (as defined by PHP), which can be evaluated without further context. If a subexpression is not of this type, a user-provided fallback evaluator is invoked. To support all constant expressions that are also supported by PHP (and not already handled by this class), the fallback evaluator must be able to handle the following node types:
- All Scalar\MagicConst* nodes.
- Expr\ConstFetch nodes. Only null/false/true are already handled by this class.
- Expr\ClassConstFetch nodes.
The fallback evaluator should throw ConstExprEvaluationException for nodes it cannot evaluate.
The evaluation is dependent on runtime configuration in two respects: Firstly, floating point to string conversions are affected by the precision ini setting. Secondly, they are also affected by the LC_NUMERIC locale.
Table of Contents
Properties
- $fallbackEvaluator : callable|null
Methods
- __construct() : mixed
- Create a constant expression evaluator.
- evaluateDirectly() : mixed
- Directly evaluates a constant expression into a PHP value.
- evaluateSilently() : mixed
- Silently evaluates a constant expression into a PHP value.
- evaluate() : mixed
- evaluateArray() : array<string|int, mixed>
- evaluateBinaryOp() : mixed
- evaluateConstFetch() : mixed
- evaluateTernary() : mixed
Properties
$fallbackEvaluator
private
callable|null
$fallbackEvaluator
Methods
__construct()
Create a constant expression evaluator.
public
__construct([callable|null $fallbackEvaluator = null ]) : mixed
The provided fallback evaluator is invoked whenever a subexpression cannot be evaluated. See class doc comment for more information.
Parameters
- $fallbackEvaluator : callable|null = null
-
To call if subexpression cannot be evaluated
evaluateDirectly()
Directly evaluates a constant expression into a PHP value.
public
evaluateDirectly(Expr $expr) : mixed
May generate Error exceptions, warnings or notices. Use evaluateSilently() to convert these into a ConstExprEvaluationException.
If some part of the expression cannot be evaluated, the fallback evaluator passed to the constructor will be invoked. By default, if no fallback is provided, an exception of type ConstExprEvaluationException is thrown.
See class doc comment for caveats and limitations.
Parameters
- $expr : Expr
-
Constant expression to evaluate
Tags
Return values
mixed —Result of evaluation
evaluateSilently()
Silently evaluates a constant expression into a PHP value.
public
evaluateSilently(Expr $expr) : mixed
Thrown Errors, warnings or notices will be converted into a ConstExprEvaluationException. The original source of the exception is available through getPrevious().
If some part of the expression cannot be evaluated, the fallback evaluator passed to the constructor will be invoked. By default, if no fallback is provided, an exception of type ConstExprEvaluationException is thrown.
See class doc comment for caveats and limitations.
Parameters
- $expr : Expr
-
Constant expression to evaluate
Tags
Return values
mixed —Result of evaluation
evaluate()
private
evaluate(Expr $expr) : mixed
Parameters
- $expr : Expr
evaluateArray()
private
evaluateArray(Array_ $expr) : array<string|int, mixed>
Parameters
- $expr : Array_
Return values
array<string|int, mixed>evaluateBinaryOp()
private
evaluateBinaryOp(BinaryOp $expr) : mixed
Parameters
- $expr : BinaryOp
evaluateConstFetch()
private
evaluateConstFetch(ConstFetch $expr) : mixed
Parameters
- $expr : ConstFetch
evaluateTernary()
private
evaluateTernary(Ternary $expr) : mixed
Parameters
- $expr : Ternary