Since: 8.0

class PhpToken implements Stringable (View source)

A class for working with PHP tokens, which is an alternative to the {token_get_all()} function.

Properties

int $id

One of the T_* constants, or an integer < 256 representing a single-char token.

string $text

The textual content of the token.

int $line

The starting line number (1-based) of the token.

int $pos

The starting position (0-based) in the tokenized string.

Methods

__construct(int $id, string $text, int $line = -1, int $pos = -1)

No description

string|null
getTokenName()

Get the name of the token.

static array
tokenize(string $code, int $flags = 0)

Same as {token_get_all()}, but returning array of {PhpToken} or an instance of a child class.

bool
is(int|string|array $kind)

Whether the token has the given ID, the given text, or has an ID/text part of the given array.

bool
isIgnorable()

Whether this token would be ignored by the PHP parser.

string
__toString()

Magic method {https://www.php.net/manual/en/language.oop5.magic.php#object.tostring} allows a class to decide how it will react when it is treated like a string.

Details

final __construct(int $id, string $text, int $line = -1, int $pos = -1)

No description

Parameters

int $id

An integer identifier

string $text

Textual content

int $line

Strating line

int $pos

Straring position (line offset)

string|null getTokenName()

Get the name of the token.

Return Value

string|null

static array tokenize(string $code, int $flags = 0)

Same as {token_get_all()}, but returning array of {PhpToken} or an instance of a child class.

Parameters

string $code

An a PHP source code

int $flags

Return Value

array

bool is(int|string|array $kind)

Whether the token has the given ID, the given text, or has an ID/text part of the given array.

Parameters

int|string|array $kind

Return Value

bool

bool isIgnorable()

Whether this token would be ignored by the PHP parser.

Return Value

bool

string __toString()

Magic method {https://www.php.net/manual/en/language.oop5.magic.php#object.tostring} allows a class to decide how it will react when it is treated like a string.

Return Value

string

Returns string representation of the object that implements this interface (and/or "__toString" magic method).