Skip to content

Instantly share code, notes, and snippets.

@shin1x1
Last active August 22, 2020 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shin1x1/6e3010905c9c8405b6eae08d705015ff to your computer and use it in GitHub Desktop.
Save shin1x1/6e3010905c9c8405b6eae08d705015ff to your computer and use it in GitHub Desktop.

https://wiki.php.net/rfc/namespaced_names_as_token

$ docker run --rm php:8.0.0beta2-cli-alpine php -r 'var_dump(token_get_all("<?php namespace new\Foo;"));'
array(5) {
  [0]=>
  array(3) {
    [0]=>
    int(390)
    [1]=>
    string(6) "<?php "
    [2]=>
    int(1)
  }
  [1]=>
  array(3) {
    [0]=>
    int(371)
    [1]=>
    string(9) "namespace"
    [2]=>
    int(1)
  }
  [2]=>
  array(3) {
    [0]=>
    int(393)
    [1]=>
    string(1) " "
    [2]=>
    int(1)
  }
  [3]=>
  array(3) {
    [0]=>
    int(314)
    [1]=>
    string(7) "new\Foo"
    [2]=>
    int(1)
  }
  [4]=>
  string(1) ";"
}
$ docker run --rm php:7.4-cli-alpine php -r 'var_dump(token_get_all("<?php namespace new\Foo;"));'
array(7) {
  [0]=>
  array(3) {
    [0]=>
    int(382)
    [1]=>
    string(6) "<?php "
    [2]=>
    int(1)
  }
  [1]=>
  array(3) {
    [0]=>
    int(391)
    [1]=>
    string(9) "namespace"
    [2]=>
    int(1)
  }
  [2]=>
  array(3) {
    [0]=>
    int(385)
    [1]=>
    string(1) " "
    [2]=>
    int(1)
  }
  [3]=>
  array(3) {
    [0]=>
    int(304)
    [1]=>
    string(3) "new"
    [2]=>
    int(1)
  }
  [4]=>
  array(3) {
    [0]=>
    int(393)
    [1]=>
    string(1) "\"
    [2]=>
    int(1)
  }
  [5]=>
  array(3) {
    [0]=>
    int(311)
    [1]=>
    string(3) "Foo"
    [2]=>
    int(1)
  }
  [6]=>
  string(1) ";"
}
@shin1x1
Copy link
Author

shin1x1 commented Aug 22, 2020

$ docker run --rm php:8.0.0beta2-cli-alpine php -r '$code = "<?php namespace namespace\Foo;"; foreach (PhpToken::getAll($code) as $t) { echo $t->getTokenName().":".$t->text."\n"; }'
T_OPEN_TAG:<?php
T_NAMESPACE:namespace
T_WHITESPACE:
T_NAME_RELATIVE:namespace\Foo
;:;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment