Copied!

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.

CloneableInstantiable
Methods
public __construct(?callable $fallbackEvaluator = NULL)
 

Create a constant expression evaluator.

The provided fallback evaluator is invoked whenever a subexpression cannot be evaluated. See
class doc comment for more information.

  • param callable|null $fallbackEvaluator To call if subexpression cannot be evaluated
public evaluateDirectly(PhpParser\Node\Expr $expr)
 

Directly evaluates a constant expression into a PHP value.

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.

  • param \Expr $expr Constant expression to evaluate
  • return mixed Result of evaluation
  • throws \ConstExprEvaluationException if the expression cannot be evaluated
public evaluateSilently(PhpParser\Node\Expr $expr)
 

Silently evaluates a constant expression into a PHP value.

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.

  • param \Expr $expr Constant expression to evaluate
  • return mixed Result of evaluation
  • throws \ConstExprEvaluationException if the expression cannot be evaluated or an error occurred
Properties
private $fallbackEvaluator = NULL
Methods
private evaluate(PhpParser\Node\Expr $expr)
private evaluateArray(PhpParser\Node\Expr\Array_ $expr)
private evaluateBinaryOp(PhpParser\Node\Expr\BinaryOp $expr)
private evaluateConstFetch(PhpParser\Node\Expr\ConstFetch $expr)
private evaluateTernary(PhpParser\Node\Expr\Ternary $expr)
© 2023 Bruce Wells
Search Namespaces \ Classes
Configuration