Re: [VOTE] Object-based token_get_all() alternative
| From: | Nikita Popov | Date: | Wed, 11 Mar 2020 09:57:16 +0000 |
| Subject: | Re: [VOTE] Object-based token_get_all() alternative | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108972@lists.php.net to get a copy of this message | ||
On Tue, Mar 10, 2020 at 5:19 PM Côme Chilliet <
come.chilliet@fusiondirectory.org> wrote:
> Le mardi 10 mars 2020, 15:53:41 CET Nikita Popov a écrit :
> > Would __toString() return $token->text? I'm not strictly opposed to this,
> > but I'd be hard pressed to think of a situation where I would prefer
> > writing (string) $token over $token->text. Generally, the less magic the
> > better :)
>
> My idea was that since tokens are parsed from a string it should be easy
> to write a string back with them.
> I do not have an obvious usecase but the idea is doing stuff like
>
> if ($token.is('function')) {
> echo "public $token";
> } else {
> echo $token;
> }
>
> Am I right to assume that with __toString the same as ->text we’d have
> implode('', PhpToken::getAll($code)) == $code ?
>
> This would allow:
> $tokens = PhpToken::getAll($code);
> // Modifications on $tokens
> $code = implode('', $tokens);
>
Yes, that would work. It's not much harder without __toString though:
$code = implode(array_column($tokens, 'text'));
Does anyone else have thoughts on whether PhpToken should implement
__toString() or not?
Nikita