Skip to content

Visual documentation of PHP Parser nodes, to help you learn AST, how to create nodes and analyse them

Notifications You must be signed in to change notification settings

rectorphp/php-parser-nodes-docs

Repository files navigation

Node Overview

Here you can find overview of commonly used nodes and how to build PHP code from them. For all nodes, check php-parser code.

PhpParser\Node\Const_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Const_; usePhpParser\Node\Scalar\String_; returnnewConst_('CONSTANT_NAME', newString_('default'));

CONSTANT_NAME = 'default'

Public Properties

  • $name - /** @var Identifier Name */
  • $value - /** @var Expr Value */
  • $namespacedName - /** @var Name|null Namespaced name (if using NameResolver) */

PhpParser\Node\Expr\ArrayDimFetch

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ArrayDimFetch; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\LNumber; $variable = newVariable('variableName'); $dimension = newLNumber(0); returnnewArrayDimFetch($variable, $dimension);

$variableName[0]

Public Properties

  • $var - /** @var Expr Variable */
  • $dim - /** @var null|Expr Array index / dim */

PhpParser\Node\Expr\ArrayItem

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ArrayItem; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\String_; $value = newVariable('Tom'); $key = newString_('name'); returnnewArrayItem($value, $key);

'name' => $Tom

Public Properties

  • $key - /** @var null|Expr Key */
  • $value - /** @var Expr Value */
  • $byRef - /** @var bool Whether to assign by reference */
  • $unpack - /** @var bool Whether to unpack the argument */

PhpParser\Node\Expr\Array_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Array_; usePhpParser\Node\Expr\ArrayItem; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\String_; $value = newVariable('Tom'); $key = newString_('name'); $arrayItem = newArrayItem($value, $key); returnnewArray_([$arrayItem]);

array('name' => $Tom)

Public Properties

  • $items - /** @var (ArrayItem|null)[] Items */

PhpParser\Node\Expr\ArrowFunction

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ArrowFunction; usePhpParser\Node\Scalar\LNumber; $subNodes['expr'] = newLNumber(1); returnnewArrowFunction($subNodes);

fn() => 1

Public Properties

  • $static - /** @var bool */
  • $byRef - /** @var bool */
  • $params - /** @var Node\Param[] */
  • $returnType - /** @var null|Node\Identifier|Node\Name|Node\ComplexType */
  • $expr - /** @var Expr */
  • $attrGroups - /** @var Node\AttributeGroup[] */

PhpParser\Node\Expr\Assign

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Assign; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\String_; $variable = newVariable('variableName'); $value = newString_('some value'); returnnewAssign($variable, $value);

$variableName = 'some value'

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Assign; usePhpParser\Node\Expr\PropertyFetch; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\String_; $propertyFetch = newPropertyFetch(newVariable('someObject'), 'someProperty'); $value = newString_('some value'); returnnewAssign($propertyFetch, $value);

$someObject->someProperty = 'some value'

Public Properties

  • $var - /** @var Expr Variable */
  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\AssignOp\Coalesce

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\AssignOp\Coalesce; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewCoalesce($left, $right);

5 ??= 10

Public Properties

  • $var - /** @var Expr Variable */
  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\AssignOp\Concat

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\AssignOp\Concat; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewConcat($left, $right);

5 .= 10

Public Properties

  • $var - /** @var Expr Variable */
  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\BinaryOp\BooleanAnd

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\BooleanAnd; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewBooleanAnd($left, $right);

5 && 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\Coalesce

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\Coalesce; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewCoalesce($left, $right);

5 ?? 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\Concat

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\Concat; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewConcat($left, $right);

5 . 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\Equal

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\Equal; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewEqual($left, $right);

5 == 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\Identical

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\Identical; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewIdentical($left, $right);

5 === 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\Minus

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\Minus; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewMinus($left, $right);

5 - 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\NotEqual

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\NotEqual; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewNotEqual($left, $right);

5 != 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\NotIdentical

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\NotIdentical; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewNotIdentical($left, $right);

5 !== 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BinaryOp\Spaceship

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BinaryOp\Spaceship; usePhpParser\Node\Scalar\LNumber; $left = newLNumber(5); $right = newLNumber(10); returnnewSpaceship($left, $right);

5 <=> 10

Public Properties

  • $left - /** @var Expr The left hand side expression */
  • $right - /** @var Expr The right hand side expression */

PhpParser\Node\Expr\BooleanNot

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\BooleanNot; usePhpParser\Node\Expr\Variable; $variable = newVariable('isEligible'); returnnewBooleanNot($variable);

!$isEligible

Public Properties

  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\Cast\Array_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Cast\Array_; usePhpParser\Node\Expr\Variable; $expr = newVariable('variableName'); returnnewArray_($expr);

(array) $variableName

Public Properties

  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\Cast\Bool_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Cast\Bool_; usePhpParser\Node\Expr\Variable; $expr = newVariable('variableName'); returnnewBool_($expr);

(bool) $variableName

Public Properties

  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\Cast\Int_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Cast\Int_; usePhpParser\Node\Expr\Variable; $expr = newVariable('variableName'); returnnewInt_($expr);

(int) $variableName

Public Properties

  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\Cast\String_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Cast\String_; usePhpParser\Node\Expr\Variable; $expr = newVariable('variableName'); returnnewString_($expr);

(string) $variableName

Public Properties

  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\ClassConstFetch

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ClassConstFetch; usePhpParser\Node\Name\FullyQualified; $class = newFullyQualified('SomeClassName'); returnnewClassConstFetch($class, 'SOME_CONSTANT');

\SomeClassName::SOME_CONSTANT

Public Properties

  • $class - /** @var Name|Expr Class name */
  • $name - /** @var Identifier|Error Constant name */

PhpParser\Node\Expr\ClosureUse

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ClosureUse; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewClosureUse($variable);

$variableName

Public Properties

  • $var - /** @var Expr\Variable Variable to use */
  • $byRef - /** @var bool Whether to use by reference */

PhpParser\Node\Expr\ConstFetch

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ConstFetch; usePhpParser\Node\Name; $name = newName('true'); returnnewConstFetch($name);

true

Public Properties

  • $name - /** @var Name Constant name */

PhpParser\Node\Expr\Empty_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Empty_; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewEmpty_($variable);

empty($variableName)

Public Properties

  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\Eval_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Eval_; usePhpParser\Node\Scalar\String_; $string = newString_('Some php code'); returnnewEval_(newString_('Some php code'));

eval('Some php code')

Public Properties

  • $expr - /** @var Expr Expression */

PhpParser\Node\Expr\FuncCall

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Arg; usePhpParser\Node\Expr\FuncCall; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Name; $args = [newArg(newVariable('someVariable'))]; returnnewFuncCall(newName('func_call'), $args);

func_call($someVariable)

Public Properties

  • $name - /** @var Node\Name|Expr Function name */
  • $args - /** @var array<Node\Arg|Node\VariadicPlaceholder> Arguments */

PhpParser\Node\Expr\Include_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Include_; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewInclude_($variable, Include_::TYPE_INCLUDE);

include$variableName

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Include_; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewInclude_($variable, Include_::TYPE_REQUIRE_ONCE);

require_once$variableName

Public Properties

  • $expr - /** @var Expr Expression */
  • $type - /** @var int Type of include */

PhpParser\Node\Expr\Instanceof_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Instanceof_; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Name\FullyQualified; $variable = newVariable('variableName'); $class = newFullyQualified('SomeClassName'); returnnewInstanceof_($variable, $class);

$variableNameinstanceof \SomeClassName

Public Properties

  • $expr - /** @var Expr Expression */
  • $class - /** @var Name|Expr Class name */

PhpParser\Node\Expr\Isset_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Isset_; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewIsset_([$variable]);

isset($variableName)

Public Properties

  • $vars - /** @var Expr[] Variables */

PhpParser\Node\Expr\List_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ArrayItem; usePhpParser\Node\Expr\List_; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); $anotherVariable = newVariable('anotherVariableName'); $arrayItems = [newArrayItem($variable), newArrayItem($anotherVariable)]; returnnewList_($arrayItems);

list($variableName, $anotherVariableName)

Public Properties

  • $items - /** @var (ArrayItem|null)[] List of items to assign to */

PhpParser\Node\Expr\Match_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Match_; usePhpParser\Node\Expr\Variable; usePhpParser\Node\MatchArm; usePhpParser\Node\Scalar\LNumber; usePhpParser\Node\Scalar\String_; $variable = newVariable('variableName'); $body = newString_('yes'); $cond = newLNumber(1); $matchArm = newMatchArm([$cond], $body); returnnewMatch_($variable, [$matchArm]);

match ($variableName) { 1 => 'yes', }

Public Properties

  • $cond - /** @var Node\Expr */
  • $arms - /** @var MatchArm[] */

PhpParser\Node\Expr\MethodCall

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\MethodCall; usePhpParser\Node\Expr\Variable; $variable = newVariable('someObject'); returnnewMethodCall($variable, 'methodName');

$someObject->methodName()

<?phpdeclare(strict_types=1); usePhpParser\Node\Arg; usePhpParser\Node\Expr\MethodCall; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\String_; $variable = newVariable('someObject'); $args = []; $args[] = newArg(newString_('yes')); $methodCall = newMethodCall($variable, 'methodName', $args); $nestedMethodCall = newMethodCall($methodCall, 'nextMethodName'); return$nestedMethodCall;

$someObject->methodName('yes')->nextMethodName()

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\MethodCall; usePhpParser\Node\Expr\PropertyFetch; usePhpParser\Node\Expr\Variable; $thisVariable = newVariable('this'); $propertyFetch = newPropertyFetch($thisVariable, 'someProperty'); returnnewMethodCall($propertyFetch, 'methodName');

$this->someProperty->methodName()

<?phpdeclare(strict_types=1); usePhpParser\Node\Arg; usePhpParser\Node\Expr\MethodCall; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\String_; $variable = newVariable('someObject'); $args = []; $args[] = newArg(newString_('yes')); $args[] = newArg(newString_('maybe')); returnnewMethodCall($variable, 'methodName', $args);

$someObject->methodName('yes', 'maybe')

Public Properties

  • $var - /** @var Expr Variable holding object */
  • $name - /** @var Identifier|Expr Method name */
  • $args - /** @var array<Arg|VariadicPlaceholder> Arguments */

PhpParser\Node\Expr\New_

Example PHP Code

<?phpdeclare(strict_types=1); // anonymous classusePhpParser\Node\Expr\New_; usePhpParser\Node\Stmt\Class_; $class = newClass_(null); returnnewNew_($class);

newclass { }

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\New_; usePhpParser\Node\Name; $class = newName('SomeClass'); returnnewNew_($class);

newSomeClass()

Public Properties

  • $class - /** @var Node\Name|Expr|Node\Stmt\Class_ Class name */
  • $args - /** @var array<Arg|VariadicPlaceholder> Arguments */

PhpParser\Node\Expr\NullsafeMethodCall

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\NullsafeMethodCall; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewNullsafeMethodCall($variable, 'methodName');

$variableName?->methodName()

Public Properties

  • $var - /** @var Expr Variable holding object */
  • $name - /** @var Identifier|Expr Method name */
  • $args - /** @var array<Arg|VariadicPlaceholder> Arguments */

PhpParser\Node\Expr\NullsafePropertyFetch

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\NullsafePropertyFetch; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewNullsafePropertyFetch($variable, 'someProperty');

$variableName?->someProperty

Public Properties

  • $var - /** @var Expr Variable holding object */
  • $name - /** @var Identifier|Expr Property name */

PhpParser\Node\Expr\PropertyFetch

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\PropertyFetch; usePhpParser\Node\Expr\Variable; $variable = newVariable('variableName'); returnnewPropertyFetch($variable, 'propertyName');

$variableName->propertyName

Public Properties

  • $var - /** @var Expr Variable holding object */
  • $name - /** @var Identifier|Expr Property name */

PhpParser\Node\Expr\StaticCall

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\StaticCall; usePhpParser\Node\Name\FullyQualified; $fullyQualified = newFullyQualified('ClassName'); returnnewStaticCall($fullyQualified, 'methodName');

\ClassName::methodName()

Public Properties

  • $class - /** @var Node\Name|Expr Class name */
  • $name - /** @var Identifier|Expr Method name */
  • $args - /** @var array<Arg|VariadicPlaceholder> Arguments */

PhpParser\Node\Expr\StaticPropertyFetch

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\StaticPropertyFetch; usePhpParser\Node\Name\FullyQualified; $class = newFullyQualified('StaticClassName'); returnnewStaticPropertyFetch($class, 'someProperty');

\StaticClassName::$someProperty

Public Properties

  • $class - /** @var Name|Expr Class name */
  • $name - /** @var VarLikeIdentifier|Expr Property name */

PhpParser\Node\Expr\Ternary

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ConstFetch; usePhpParser\Node\Expr\Ternary; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Name; $variable = newVariable('variableName'); $trueConstFetch = newConstFetch(newName('true')); $falseConstFetch = newConstFetch(newName('false')); returnnewTernary($variable, $trueConstFetch, $falseConstFetch);

$variableName ? true : false

Public Properties

  • $cond - /** @var Expr Condition */
  • $if - /** @var null|Expr Expression for true */
  • $else - /** @var Expr Expression for false */

PhpParser\Node\Expr\Throw_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Throw_; usePhpParser\Node\Scalar\String_; $string = newString_('some string'); returnnewThrow_($string);

throw'some string'

Public Properties

  • $expr - /** @var Node\Expr Expression */

PhpParser\Node\Expr\Variable

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; returnnewVariable('variableName');

$variableName

Public Properties

  • $name - /** @var string|Expr Name */

PhpParser\Node\MatchArm

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\MatchArm; usePhpParser\Node\Scalar\LNumber; usePhpParser\Node\Scalar\String_; $conds = [newLNumber(1)]; $body = newString_('yes'); returnnewMatchArm($conds, $body);

1 => 'yes'

Public Properties

  • $conds - /** @var null|Node\Expr[] */
  • $body - /** @var Node\Expr */

PhpParser\Node\Name

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Name; returnnewName('shortName');

shortName

Public Properties

  • $parts - `/**
    • @var string[] Parts of the name
    • @deprecated Use getParts() instead */`
  • $specialClassNames - ``

PhpParser\Node\Name\FullyQualified

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Name\FullyQualified; returnnewFullyQualified('SomeNamespace\ShortName');

\SomeNamespace\ShortName

Public Properties

  • $parts - `/**
    • @var string[] Parts of the name
    • @deprecated Use getParts() instead */`

PhpParser\Node\NullableType

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\NullableType; returnnewNullableType('SomeType');

?SomeType

Public Properties

  • $type - /** @var Identifier|Name Type */

PhpParser\Node\Param

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Param; $variable = newVariable('variableName'); returnnewParam($variable);

$variableName

Public Properties

  • $type - /** @var null|Identifier|Name|ComplexType Type declaration */
  • $byRef - /** @var bool Whether parameter is passed by reference */
  • $variadic - /** @var bool Whether this is a variadic argument */
  • $var - /** @var Expr\Variable|Expr\Error Parameter variable */
  • $default - /** @var null|Expr Default value */
  • $flags - /** @var int */
  • $attrGroups - /** @var AttributeGroup[] PHP attribute groups */

PhpParser\Node\Scalar\DNumber

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Scalar\DNumber; returnnewDNumber(10.5);

10.5

Public Properties

  • $value - /** @var float Number value */

PhpParser\Node\Scalar\Encapsed

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\Encapsed; returnnewEncapsed([newVariable('variableName')]);

"{$variableName}"

Public Properties

  • $parts - /** @var Expr[] list of string parts */

PhpParser\Node\Scalar\LNumber

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Scalar\LNumber; returnnewLNumber(1000);

1000

Public Properties

  • $value - /** @var int Number value */

PhpParser\Node\Scalar\String_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Scalar\String_; returnnewString_('some string');

'some string'

Public Properties

  • $value - /** @var string String value */
  • $replacements - ``

PhpParser\Node\Stmt\ClassConst

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Const_; usePhpParser\Node\Scalar\String_; usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\ClassConst; $defaultValue = newString_('default value'); $const = newConst_('SOME_CLASS_CONSTANT', $defaultValue); returnnewClassConst([$const], Class_::MODIFIER_PUBLIC);

publicconstSOME_CLASS_CONSTANT = 'default value';

Public Properties

  • $flags - /** @var int Modifiers */
  • $consts - /** @var Node\Const_[] Constant declarations */
  • $attrGroups - /** @var Node\AttributeGroup[] */

PhpParser\Node\Stmt\ClassMethod

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\ClassMethod; $classMethod = newClassMethod('methodName'); $classMethod->flags = Class_::MODIFIER_PUBLIC; return$classMethod;

publicfunctionmethodName() { }

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Identifier; usePhpParser\Node\Param; usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\ClassMethod; $classMethod = newClassMethod('methodName'); $classMethod->flags = Class_::MODIFIER_PRIVATE; $param = newParam(newVariable('paramName')); $classMethod->params = [$param]; $classMethod->returnType = newIdentifier('string'); return$classMethod;

privatefunctionmethodName($paramName) : string { }

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Assign; usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\LNumber; usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\ClassMethod; usePhpParser\Node\Stmt\Expression; $classMethod = newClassMethod('methodName'); $classMethod->flags = Class_::MODIFIER_PUBLIC; $variable = newVariable('some'); $number = newLNumber(10000); $assign = newAssign($variable, $number); $classMethod->stmts[] = newExpression($assign); return$classMethod;

publicfunctionmethodName() { $some = 10000; }

Public Properties

  • $flags - /** @var int Flags */
  • $byRef - /** @var bool Whether to return by reference */
  • $name - /** @var Node\Identifier Name */
  • $params - /** @var Node\Param[] Parameters */
  • $returnType - /** @var null|Node\Identifier|Node\Name|Node\ComplexType Return type */
  • $stmts - /** @var Node\Stmt[]|null Statements */
  • $attrGroups - /** @var Node\AttributeGroup[] PHP attribute groups */
  • $magicNames - ``

PhpParser\Node\Stmt\Class_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Class_; returnnewClass_('ClassName');

class ClassName { }

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Class_; $class = newClass_('ClassName'); $class->flags |= Class_::MODIFIER_FINAL; return$class;

finalclass ClassName { }

<?phpdeclare(strict_types=1); usePhpParser\Node\Name\FullyQualified; usePhpParser\Node\Stmt\Class_; $class = newClass_('ClassName'); $class->flags = Class_::MODIFIER_FINAL; $class->extends = newFullyQualified('ParentClass'); return$class;

finalclass ClassName extends \ParentClass { }

Public Properties

  • $flags - /** @var int Type */
  • $extends - /** @var null|Node\Name Name of extended class */
  • $implements - /** @var Node\Name[] Names of implemented interfaces */
  • $name - /** @var Node\Identifier|null Name */
  • $stmts - /** @var Node\Stmt[] Statements */
  • $attrGroups - /** @var Node\AttributeGroup[] PHP attribute groups */
  • $namespacedName - /** @var Node\Name|null Namespaced name (if using NameResolver) */

PhpParser\Node\Stmt\Const_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Const_; usePhpParser\Node\Scalar\String_; usePhpParser\Node\Stmt\Const_asConstStmt; $consts = [newConst_('CONSTANT_IN_CLASS', newString_('default value'))]; returnnewConstStmt($consts);

constCONSTANT_IN_CLASS = 'default value';

Public Properties

  • $consts - /** @var Node\Const_[] Constant declarations */

PhpParser\Node\Stmt\Declare_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Scalar\LNumber; usePhpParser\Node\Stmt\Declare_; usePhpParser\Node\Stmt\DeclareDeclare; $declareDeclare = newDeclareDeclare('strict_types', newLNumber(1)); returnnewDeclare_([$declareDeclare]);

declare (strict_types=1);

Public Properties

  • $declares - /** @var DeclareDeclare[] List of declares */
  • $stmts - /** @var Node\Stmt[]|null Statements */

PhpParser\Node\Stmt\Do_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Stmt\Do_; $variable = newVariable('variableName'); returnnewDo_($variable);

do { } while ($variableName);

Public Properties

  • $stmts - /** @var Node\Stmt[] Statements */
  • $cond - /** @var Node\Expr Condition */

PhpParser\Node\Stmt\Echo_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Scalar\String_; usePhpParser\Node\Stmt\Echo_; $string = newString_('hello'); returnnewEcho_([$string]);

echo'hello';

Public Properties

  • $exprs - /** @var Node\Expr[] Expressions */

PhpParser\Node\Stmt\ElseIf_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ConstFetch; usePhpParser\Node\Name; usePhpParser\Node\Stmt\ElseIf_; usePhpParser\Node\Stmt\Return_; $name = newName('true'); $constFetch = newConstFetch($name); $stmt = newReturn_(); returnnewElseIf_($constFetch, [$stmt]);

elseif (true) { return; }

Public Properties

  • $cond - /** @var Node\Expr Condition */
  • $stmts - /** @var Node\Stmt[] Statements */

PhpParser\Node\Stmt\Foreach_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Stmt\Foreach_; $foreachedVariable = newVariable('foreachedVariableName'); $asVariable = newVariable('asVariable'); returnnewForeach_($foreachedVariable, $asVariable);

foreach ($foreachedVariableNameas$asVariable) { }

Public Properties

  • $expr - /** @var Node\Expr Expression to iterate */
  • $keyVar - /** @var null|Node\Expr Variable to assign key to */
  • $byRef - /** @var bool Whether to assign value by reference */
  • $valueVar - /** @var Node\Expr Variable to assign value to */
  • $stmts - /** @var Node\Stmt[] Statements */

PhpParser\Node\Stmt\Function_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Function_; returnnewFunction_('some_function');

functionsome_function() { }

Public Properties

  • $byRef - /** @var bool Whether function returns by reference */
  • $name - /** @var Node\Identifier Name */
  • $params - /** @var Node\Param[] Parameters */
  • $returnType - /** @var null|Node\Identifier|Node\Name|Node\ComplexType Return type */
  • $stmts - /** @var Node\Stmt[] Statements */
  • $attrGroups - /** @var Node\AttributeGroup[] PHP attribute groups */
  • $namespacedName - /** @var Node\Name|null Namespaced name (if using NameResolver) */

PhpParser\Node\Stmt\If_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\ConstFetch; usePhpParser\Node\Name; usePhpParser\Node\Stmt\If_; $cond = newConstFetch(newName('true')); returnnewIf_($cond);

if (true) { }

Public Properties

  • $cond - /** @var Node\Expr Condition expression */
  • $stmts - /** @var Node\Stmt[] Statements */
  • $elseifs - /** @var ElseIf_[] Elseif clauses */
  • $else - /** @var null|Else_ Else clause */

PhpParser\Node\Stmt\InlineHTML

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\InlineHTML; returnnewInlineHTML('<strong>feel</strong>');

?> <strong>feel</strong><?php

Public Properties

  • $value - /** @var string String */

PhpParser\Node\Stmt\Interface_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Identifier; usePhpParser\Node\Stmt\Interface_; returnnewInterface_(newIdentifier('InterfaceName'));

interface InterfaceName { }

Public Properties

  • $extends - /** @var Node\Name[] Extended interfaces */
  • $name - /** @var Node\Identifier|null Name */
  • $stmts - /** @var Node\Stmt[] Statements */
  • $attrGroups - /** @var Node\AttributeGroup[] PHP attribute groups */
  • $namespacedName - /** @var Node\Name|null Namespaced name (if using NameResolver) */

PhpParser\Node\Stmt\Label

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Label; returnnewLabel('labelName');

labelName:

Public Properties

  • $name - /** @var Identifier Name */

PhpParser\Node\Stmt\Property

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\Property; usePhpParser\Node\Stmt\PropertyProperty; usePhpParser\Node\VarLikeIdentifier; $propertyProperty = newPropertyProperty(newVarLikeIdentifier('propertyName')); returnnewProperty(Class_::MODIFIER_PUBLIC, [$propertyProperty], [], 'string');

public string $propertyName;

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\Property; usePhpParser\Node\Stmt\PropertyProperty; usePhpParser\Node\VarLikeIdentifier; $propertyProperty = newPropertyProperty(newVarLikeIdentifier('propertyName')); returnnewProperty(Class_::MODIFIER_PUBLIC, [$propertyProperty]);

public$propertyName;

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\Property; usePhpParser\Node\Stmt\PropertyProperty; $propertyProperties = [newPropertyProperty('firstProperty'), newPropertyProperty('secondProperty')]; returnnewProperty(Class_::MODIFIER_STATIC | Class_::MODIFIER_PUBLIC, $propertyProperties);

publicstatic$firstProperty, $secondProperty;

Public Properties

  • $flags - /** @var int Modifiers */
  • $props - /** @var PropertyProperty[] Properties */
  • $type - /** @var null|Identifier|Name|ComplexType Type declaration */
  • $attrGroups - /** @var Node\AttributeGroup[] PHP attribute groups */

PhpParser\Node\Stmt\PropertyProperty

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\Property; usePhpParser\Node\Stmt\PropertyProperty; $class = newClass_('ClassName'); $propertyProperty = newPropertyProperty('someProperty'); $property = newProperty(Class_::MODIFIER_PRIVATE, [$propertyProperty]); $class->stmts[] = $property; return$propertyProperty;

$someProperty

Public Properties

  • $name - /** @var Node\VarLikeIdentifier Name */
  • $default - /** @var null|Node\Expr Default */

PhpParser\Node\Stmt\StaticVar

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Stmt\StaticVar; $variable = newVariable('variableName'); returnnewStaticVar($variable);

$variableName

Public Properties

  • $var - /** @var Expr\Variable Variable */
  • $default - /** @var null|Node\Expr Default value */

PhpParser\Node\Stmt\Static_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Stmt\Static_; usePhpParser\Node\Stmt\StaticVar; $staticVars = [newStaticVar(newVariable('static'))]; returnnewStatic_($staticVars);

static$static;

Public Properties

  • $vars - /** @var StaticVar[] Variable definitions */

PhpParser\Node\Stmt\Switch_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Scalar\LNumber; usePhpParser\Node\Stmt\Case_; usePhpParser\Node\Stmt\Switch_; $cond = newVariable('variableName'); $cases = [newCase_(newLNumber(1))]; returnnewSwitch_($cond, $cases);

switch ($variableName) { case1: }

Public Properties

  • $cond - /** @var Node\Expr Condition */
  • $cases - /** @var Case_[] Case list */

PhpParser\Node\Stmt\Throw_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Scalar\String_; usePhpParser\Node\Stmt\Throw_; $string = newString_('some string'); returnnewThrow_($string);

throw'some string';

Public Properties

  • $expr - /** @var Node\Expr Expression */

PhpParser\Node\Stmt\TraitUse

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Name\FullyQualified; usePhpParser\Node\Stmt\TraitUse; returnnewTraitUse([newFullyQualified('TraitName')]);

use \TraitName;

Public Properties

  • $traits - /** @var Node\Name[] Traits */
  • $adaptations - /** @var TraitUseAdaptation[] Adaptations */

PhpParser\Node\Stmt\TraitUseAdaptation\Alias

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Name\FullyQualified; usePhpParser\Node\Stmt\Class_; usePhpParser\Node\Stmt\TraitUseAdaptation\Alias; $traitFullyQualified = newFullyQualified('TraitName'); returnnewAlias($traitFullyQualified, 'method', Class_::MODIFIER_PUBLIC, 'aliasedMethod');

\TraitName::method as public aliasedMethod;

Public Properties

  • $newModifier - /** @var null|int New modifier */
  • $newName - /** @var null|Node\Identifier New name */
  • $trait - /** @var Node\Name|null Trait name */
  • $method - /** @var Node\Identifier Method name */

PhpParser\Node\Stmt\Trait_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Stmt\Trait_; returnnewTrait_('TraitName');

trait TraitName { }

Public Properties

  • $name - /** @var Node\Identifier|null Name */
  • $stmts - /** @var Node\Stmt[] Statements */
  • $attrGroups - /** @var Node\AttributeGroup[] PHP attribute groups */
  • $namespacedName - /** @var Node\Name|null Namespaced name (if using NameResolver) */

PhpParser\Node\Stmt\TryCatch

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Name\FullyQualified; usePhpParser\Node\Scalar\String_; usePhpParser\Node\Stmt\Catch_; usePhpParser\Node\Stmt\Echo_; usePhpParser\Node\Stmt\Finally_; usePhpParser\Node\Stmt\TryCatch; $echo = newEcho_([newString_('one')]); $tryStmts = [$echo]; $echo2 = newEcho_([newString_('two')]); $catch = newCatch_([newFullyQualified('SomeType')], null, [$echo2]); $echo3 = newEcho_([newString_('three')]); $finally = newFinally_([$echo3]); returnnewTryCatch($tryStmts, [$catch]);

try { echo'one'; } catch (\SomeType) { echo'two'; }

Public Properties

  • $stmts - /** @var Node\Stmt[] Statements */
  • $catches - /** @var Catch_[] Catches */
  • $finally - /** @var null|Finally_ Optional finally node */

PhpParser\Node\Stmt\Unset_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Stmt\Unset_; $variable = newVariable('variableName'); returnnewUnset_([$variable]);

unset($variableName);

Public Properties

  • $vars - /** @var Node\Expr[] Variables to unset */

PhpParser\Node\Stmt\Use_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Name; usePhpParser\Node\Stmt\Use_; usePhpParser\Node\Stmt\UseUse; $useUse = newUseUse(newName('UsedNamespace')); returnnewUse_([$useUse]);

useUsedNamespace;

Public Properties

  • $type - /** @var int Type of alias */
  • $uses - /** @var UseUse[] Aliases */

PhpParser\Node\Stmt\While_

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Expr\Variable; usePhpParser\Node\Stmt\While_; returnnewWhile_(newVariable('variableName'));

while ($variableName) { }

Public Properties

  • $cond - /** @var Node\Expr Condition */
  • $stmts - /** @var Node\Stmt[] Statements */

PhpParser\Node\UnionType

Example PHP Code

<?phpdeclare(strict_types=1); usePhpParser\Node\Identifier; usePhpParser\Node\UnionType; $unionedTypes = [newIdentifier('string'), newIdentifier('int')]; returnnewUnionType($unionedTypes);

string|int

Public Properties

  • $types - /** @var (Identifier|Name|IntersectionType)[] Types */

About

Visual documentation of PHP Parser nodes, to help you learn AST, how to create nodes and analyse them

Topics

Resources

Stars

Watchers

Forks

Languages

close