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

<?php

declare(strict_types=1);

use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;

return new Const_('CONSTANT_NAME', new String_('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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;

$variable = new Variable('variableName');
$dimension = new LNumber(0);

return new ArrayDimFetch($variable, $dimension);

$variableName[0]

Public Properties

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

PhpParser\Node\Expr\ArrayItem

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;

$value = new Variable('Tom');
$key = new String_('name');

return new ArrayItem($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;

$value = new Variable('Tom');
$key = new String_('name');

$arrayItem = new ArrayItem($value, $key);

return new Array_([$arrayItem]);

array('name' => $Tom)

Public Properties

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

PhpParser\Node\Expr\ArrowFunction

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Scalar\LNumber;

$subNodes['expr'] = new LNumber(1);

return new ArrowFunction($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;

$variable = new Variable('variableName');
$value = new String_('some value');

return new Assign($variable, $value);

$variableName = 'some value'

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;

$propertyFetch = new PropertyFetch(new Variable('someObject'), 'someProperty');
$value = new String_('some value');

return new Assign($propertyFetch, $value);

$someObject->someProperty = 'some value'

Public Properties

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

PhpParser\Node\Expr\AssignOp\Coalesce

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\AssignOp\Coalesce;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Coalesce($left, $right);

5 ??= 10

Public Properties

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

PhpParser\Node\Expr\AssignOp\Concat

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\AssignOp\Concat;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Concat($left, $right);

5 .= 10

Public Properties

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

PhpParser\Node\Expr\BinaryOp\BooleanAnd

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new BooleanAnd($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Coalesce($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Concat($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Equal($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Identical($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Minus($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new NotEqual($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new NotIdentical($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BinaryOp\Spaceship;
use PhpParser\Node\Scalar\LNumber;

$left = new LNumber(5);
$right = new LNumber(10);

return new Spaceship($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('isEligible');

return new BooleanNot($variable);

!$isEligible

Public Properties

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

PhpParser\Node\Expr\Cast\Array_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\Variable;

$expr = new Variable('variableName');

return new Array_($expr);

(array) $variableName

Public Properties

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

PhpParser\Node\Expr\Cast\Bool_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Variable;

$expr = new Variable('variableName');

return new Bool_($expr);

(bool) $variableName

Public Properties

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

PhpParser\Node\Expr\Cast\Int_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Variable;

$expr = new Variable('variableName');

return new Int_($expr);

(int) $variableName

Public Properties

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

PhpParser\Node\Expr\Cast\String_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\Variable;

$expr = new Variable('variableName');

return new String_($expr);

(string) $variableName

Public Properties

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

PhpParser\Node\Expr\ClassConstFetch

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;

$class = new FullyQualified('SomeClassName');

return new ClassConstFetch($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new ClosureUse($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;

$name = new Name('true');

return new ConstFetch($name);

true

Public Properties

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

PhpParser\Node\Expr\Empty_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new Empty_($variable);

empty($variableName)

Public Properties

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

PhpParser\Node\Expr\Eval_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Eval_;
use PhpParser\Node\Scalar\String_;

$string = new String_('Some php code');

return new Eval_(new String_('Some php code'));

eval('Some php code')

Public Properties

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

PhpParser\Node\Expr\FuncCall

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;

$args = [new Arg(new Variable('someVariable'))];

return new FuncCall(new Name('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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new Include_($variable, Include_::TYPE_INCLUDE);

include $variableName

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new Include_($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;

$variable = new Variable('variableName');
$class = new FullyQualified('SomeClassName');

return new Instanceof_($variable, $class);

$variableName instanceof \SomeClassName

Public Properties

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

PhpParser\Node\Expr\Isset_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new Isset_([$variable]);

isset($variableName)

Public Properties

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

PhpParser\Node\Expr\List_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');
$anotherVariable = new Variable('anotherVariableName');

$arrayItems = [new ArrayItem($variable), new ArrayItem($anotherVariable)];

return new List_($arrayItems);

list($variableName, $anotherVariableName)

Public Properties

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

PhpParser\Node\Expr\Match_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;

$variable = new Variable('variableName');

$body = new String_('yes');
$cond = new LNumber(1);
$matchArm = new MatchArm([$cond], $body);

return new Match_($variable, [$matchArm]);

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

Public Properties

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

PhpParser\Node\Expr\MethodCall

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('someObject');

return new MethodCall($variable, 'methodName');

$someObject->methodName()

<?php

declare(strict_types=1);

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;

$variable = new Variable('someObject');

$args = [];
$args[] = new Arg(new String_('yes'));

$methodCall = new MethodCall($variable, 'methodName', $args);

$nestedMethodCall = new MethodCall($methodCall, 'nextMethodName');

return $nestedMethodCall;

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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;

$thisVariable = new Variable('this');
$propertyFetch = new PropertyFetch($thisVariable, 'someProperty');

return new MethodCall($propertyFetch, 'methodName');

$this->someProperty->methodName()

<?php

declare(strict_types=1);

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;

$variable = new Variable('someObject');

$args = [];
$args[] = new Arg(new String_('yes'));
$args[] = new Arg(new String_('maybe'));

return new MethodCall($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

<?php

declare(strict_types=1);

// anonymous class

use PhpParser\Node\Expr\New_;
use PhpParser\Node\Stmt\Class_;

$class = new Class_(null);

return new New_($class);

new class
{
}

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;

$class = new Name('SomeClass');

return new New_($class);

new SomeClass()

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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new NullsafeMethodCall($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new NullsafePropertyFetch($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;

$variable = new Variable('variableName');

return new PropertyFetch($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;

$fullyQualified = new FullyQualified('ClassName');

return new StaticCall($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Name\FullyQualified;

$class = new FullyQualified('StaticClassName');

return new StaticPropertyFetch($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;

$variable = new Variable('variableName');

$trueConstFetch = new ConstFetch(new Name('true'));
$falseConstFetch = new ConstFetch(new Name('false'));

return new Ternary($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Scalar\String_;

$string = new String_('some string');

return new Throw_($string);

throw 'some string'

Public Properties

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

PhpParser\Node\Expr\Variable

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;

return new Variable('variableName');

$variableName

Public Properties

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

PhpParser\Node\MatchArm

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\MatchArm;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;

$conds = [new LNumber(1)];
$body = new String_('yes');

return new MatchArm($conds, $body);

1 => 'yes'

Public Properties

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

PhpParser\Node\Name

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Name;

return new Name('shortName');

shortName

Public Properties

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

PhpParser\Node\Name\FullyQualified

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Name\FullyQualified;

return new FullyQualified('SomeNamespace\ShortName');

\SomeNamespace\ShortName

Public Properties

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

PhpParser\Node\NullableType

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\NullableType;

return new NullableType('SomeType');

?SomeType

Public Properties

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

PhpParser\Node\Param

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;

$variable = new Variable('variableName');

return new Param($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

<?php

declare(strict_types=1);

use PhpParser\Node\Scalar\DNumber;

return new DNumber(10.5);

10.5

Public Properties

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

PhpParser\Node\Scalar\Encapsed

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Encapsed;

return new Encapsed([new Variable('variableName')]);

"{$variableName}"

Public Properties

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

PhpParser\Node\Scalar\LNumber

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Scalar\LNumber;

return new LNumber(1000);

1000

Public Properties

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

PhpParser\Node\Scalar\String_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Scalar\String_;

return new String_('some string');

'some string'

Public Properties

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

PhpParser\Node\Stmt\ClassConst

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;

$defaultValue = new String_('default value');
$const = new Const_('SOME_CLASS_CONSTANT', $defaultValue);

return new ClassConst([$const], Class_::MODIFIER_PUBLIC);

public const SOME_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

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;

$classMethod = new ClassMethod('methodName');
$classMethod->flags = Class_::MODIFIER_PUBLIC;

return $classMethod;

public function methodName()
{
}

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;

$classMethod = new ClassMethod('methodName');
$classMethod->flags = Class_::MODIFIER_PRIVATE;

$param = new Param(new Variable('paramName'));
$classMethod->params = [$param];
$classMethod->returnType = new Identifier('string');

return $classMethod;

private function methodName($paramName) : string
{
}

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;

$classMethod = new ClassMethod('methodName');
$classMethod->flags = Class_::MODIFIER_PUBLIC;

$variable = new Variable('some');
$number = new LNumber(10000);
$assign = new Assign($variable, $number);

$classMethod->stmts[] = new Expression($assign);

return $classMethod;

public function methodName()
{
    $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

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Class_;

return new Class_('ClassName');

class ClassName
{
}

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Class_;

$class = new Class_('ClassName');
$class->flags |= Class_::MODIFIER_FINAL;

return $class;

final class ClassName
{
}

<?php

declare(strict_types=1);

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;

$class = new Class_('ClassName');

$class->flags = Class_::MODIFIER_FINAL;
$class->extends = new FullyQualified('ParentClass');

return $class;

final class 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

<?php

declare(strict_types=1);

use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Const_ as ConstStmt;

$consts = [new Const_('CONSTANT_IN_CLASS', new String_('default value'))];

return new ConstStmt($consts);

const CONSTANT_IN_CLASS = 'default value';

Public Properties

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

PhpParser\Node\Stmt\Declare_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\DeclareDeclare;

$declareDeclare = new DeclareDeclare('strict_types', new LNumber(1));

return new Declare_([$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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Do_;

$variable = new Variable('variableName');

return new Do_($variable);

do {
} while ($variableName);

Public Properties

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

PhpParser\Node\Stmt\Echo_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Echo_;

$string = new String_('hello');

return new Echo_([$string]);

echo 'hello';

Public Properties

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

PhpParser\Node\Stmt\ElseIf_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Return_;

$name = new Name('true');
$constFetch = new ConstFetch($name);
$stmt = new Return_();

return new ElseIf_($constFetch, [$stmt]);

elseif (true) {
    return;
}

Public Properties

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

PhpParser\Node\Stmt\Foreach_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Foreach_;

$foreachedVariable = new Variable('foreachedVariableName');
$asVariable = new Variable('asVariable');

return new Foreach_($foreachedVariable, $asVariable);

foreach ($foreachedVariableName as $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

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Function_;

return new Function_('some_function');

function some_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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\If_;

$cond = new ConstFetch(new Name('true'));

return new If_($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

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\InlineHTML;

return new InlineHTML('<strong>feel</strong>');

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

Public Properties

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

PhpParser\Node\Stmt\Interface_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Interface_;

return new Interface_(new Identifier('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

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Label;

return new Label('labelName');

labelName:

Public Properties

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

PhpParser\Node\Stmt\Property

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;

$propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName'));

return new Property(Class_::MODIFIER_PUBLIC, [$propertyProperty], [], 'string');

public string $propertyName;

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;

$propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName'));

return new Property(Class_::MODIFIER_PUBLIC, [$propertyProperty]);

public $propertyName;

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;

$propertyProperties = [new PropertyProperty('firstProperty'), new PropertyProperty('secondProperty')];

return new Property(Class_::MODIFIER_STATIC | Class_::MODIFIER_PUBLIC, $propertyProperties);

public static $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

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;

$class = new Class_('ClassName');

$propertyProperty = new PropertyProperty('someProperty');
$property = new Property(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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\StaticVar;

$variable = new Variable('variableName');

return new StaticVar($variable);

$variableName

Public Properties

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

PhpParser\Node\Stmt\Static_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\StaticVar;

$staticVars = [new StaticVar(new Variable('static'))];

return new Static_($staticVars);

static $static;

Public Properties

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

PhpParser\Node\Stmt\Switch_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Switch_;

$cond = new Variable('variableName');
$cases = [new Case_(new LNumber(1))];

return new Switch_($cond, $cases);

switch ($variableName) {
    case 1:
}

Public Properties

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

PhpParser\Node\Stmt\Throw_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Throw_;

$string = new String_('some string');

return new Throw_($string);

throw 'some string';

Public Properties

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

PhpParser\Node\Stmt\TraitUse

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\TraitUse;

return new TraitUse([new FullyQualified('TraitName')]);

use \TraitName;

Public Properties

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

PhpParser\Node\Stmt\TraitUseAdaptation\Alias

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;

$traitFullyQualified = new FullyQualified('TraitName');

return new Alias($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

<?php

declare(strict_types=1);

use PhpParser\Node\Stmt\Trait_;

return new Trait_('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

<?php

declare(strict_types=1);

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\TryCatch;

$echo = new Echo_([new String_('one')]);
$tryStmts = [$echo];

$echo2 = new Echo_([new String_('two')]);
$catch = new Catch_([new FullyQualified('SomeType')], null, [$echo2]);

$echo3 = new Echo_([new String_('three')]);
$finally = new Finally_([$echo3]);

return new TryCatch($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

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Unset_;

$variable = new Variable('variableName');

return new Unset_([$variable]);

unset($variableName);

Public Properties

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

PhpParser\Node\Stmt\Use_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;

$useUse = new UseUse(new Name('UsedNamespace'));

return new Use_([$useUse]);

use UsedNamespace;

Public Properties

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

PhpParser\Node\Stmt\While_

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\While_;

return new While_(new Variable('variableName'));

while ($variableName) {
}

Public Properties

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

PhpParser\Node\UnionType

Example PHP Code

<?php

declare(strict_types=1);

use PhpParser\Node\Identifier;
use PhpParser\Node\UnionType;

$unionedTypes = [new Identifier('string'), new Identifier('int')];

return new UnionType($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