PHP code example of nikic / php-ast

1. Go to this page and download the library: Download nikic/php-ast library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nikic / php-ast example snippets


$ast = ast\parse_code(' ...', $version=120);
// or
$ast = ast\parse_file('file.php', $version=120);

namespace ast;
class Node {
    public $kind;
    public $flags;
    public $lineno;
    public $children;
}



$code = <<<'EOC'

$var = 42;
EOC;

var_dump(ast\parse_code($code, $version=70));

// Output:
object(ast\Node)#1 (4) {
  ["kind"]=>
  int(132)
  ["flags"]=>
  int(0)
  ["lineno"]=>
  int(1)
  ["children"]=>
  array(1) {
    [0]=>
    object(ast\Node)#2 (4) {
      ["kind"]=>
      int(517)
      ["flags"]=>
      int(0)
      ["lineno"]=>
      int(2)
      ["children"]=>
      array(2) {
        ["var"]=>
        object(ast\Node)#3 (4) {
          ["kind"]=>
          int(256)
          ["flags"]=>
          int(0)
          ["lineno"]=>
          int(2)
          ["children"]=>
          array(1) {
            ["name"]=>
            string(3) "var"
          }
        }
        ["expr"]=>
        int(42)
      }
    }
  }
}



 = <<<'EOC'

$var = 42;
EOC;

echo ast_dump(ast\parse_code($code, $version=70)), "\n";

// Output:
AST_STMT_LIST
    0: AST_ASSIGN
        var: AST_VAR
            name: "var"
        expr: 42

namespace ast;
class Metadata
{
    public $kind;
    public $name;
    public $flags;
    public $flagsCombinable;
}