rfc:token_as_object
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:token_as_object [2020/02/13 12:45] – Add missing "static" in example code guilliamxavier | rfc:token_as_object [2020/02/25 15:58] – nikic | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: token_get_all() | + | ====== PHP RFC: Object-based |
* Date: 2020-02-13 | * Date: 2020-02-13 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
Line 8: | Line 8: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | The '' | + | The '' |
===== Proposal ===== | ===== Proposal ===== | ||
- | '' | + | A new '' |
<PHP> | <PHP> | ||
Line 24: | Line 24: | ||
/** The starting position (0-based) in the tokenized string. */ | /** The starting position (0-based) in the tokenized string. */ | ||
public int $pos; | public int $pos; | ||
+ | | ||
+ | /** | ||
+ | * Same as token_get_all(), | ||
+ | * @return static[] | ||
+ | */ | ||
+ | public static function getAll(string $code, int $flags = 0): array; | ||
+ | | ||
+ | final public function __construct(int $id, string $text, int $line = -1, int $pos = -1); | ||
} | } | ||
</ | </ | ||
+ | |||
+ | The '' | ||
It should be emphasized that **all** tokens are returned as objects, including single-char tokens. While this uses more memory than returning them as strings, experience has shown that the current string/ | It should be emphasized that **all** tokens are returned as objects, including single-char tokens. While this uses more memory than returning them as strings, experience has shown that the current string/ | ||
Line 35: | Line 45: | ||
* The token stores the position in the file, so that consumers don't have to compute and store it separately. | * The token stores the position in the file, so that consumers don't have to compute and store it separately. | ||
- | Finally | + | Finally, the tokens take up significantly |
< | < | ||
Line 46: | Line 56: | ||
</ | </ | ||
- | ===== Open Questions ===== | + | ==== Extensibility |
- | ==== Construction method ==== | + | The '' |
- | + | ||
- | The RFC currently proposes a new '' | + | |
- | + | ||
- | To make the functionality self-contained, | + | |
<PHP> | <PHP> | ||
- | class PhpToken { | + | class MyPhpToken extends |
- | /** @return PhpToken[] */ | + | public function |
- | public | + | return strtolower($this-> |
+ | } | ||
} | } | ||
+ | |||
+ | $tokens = MyPhpToken:: | ||
+ | var_dump($tokens[0] instanceof MyPhpToken); | ||
+ | $tokens[0]-> | ||
</ | </ | ||
+ | |||
+ | To guarantee a well-defined construction behavior, the '' | ||
+ | |||
+ | ===== Open Questions ===== | ||
==== Additional methods ==== | ==== Additional methods ==== | ||
Line 105: | Line 120: | ||
/** Get the name of the token. */ | /** Get the name of the token. */ | ||
- | public function getTokenName(): | + | public function getTokenName(): |
if ($this-> | if ($this-> | ||
return chr($this-> | return chr($this-> | ||
+ | } elseif (' | ||
+ | return $name; | ||
} else { | } else { | ||
- | return | + | return |
} | } | ||
} | } | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== Rejected Features ===== | ||
+ | |||
+ | ==== Lazy token stream ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | This is not supported by the current proposal, because the current PHP lexer doesn' | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | There are no backwards compatibility breaks, apart from the new constant name and the new class name. | + | There are no backwards compatibility breaks, apart from the new class name. |
===== Vote ===== | ===== Vote ===== |
rfc/token_as_object.txt · Last modified: 2020/11/12 13:33 by nikic