PHPackages                             phpwatch/phptoken-polyfill - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpwatch/phptoken-polyfill

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phpwatch/phptoken-polyfill
==========================

A polyfill for PHP 8.0's PhpToken class

v0.0.3(5y ago)10193[1 PRs](https://github.com/PHPWatch/phptoken-polyfill/pulls)MITPHPPHP ^7.1

Since Apr 11Pushed 4y ago2 watchersCompare

[ Source](https://github.com/PHPWatch/phptoken-polyfill)[ Packagist](https://packagist.org/packages/phpwatch/phptoken-polyfill)[ Docs](https://php.watch/versions/8.0/PhpToken)[ RSS](/packages/phpwatch-phptoken-polyfill/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

PhpToken Polyfill
=================

[](#phptoken-polyfill)

A polyfill to bring the [PHP 8.0 `PhpToken`](https://php.watch/versions/8.0/PhpToken) class to PHP 7.1 and later.

[![Latest Stable Version](https://camo.githubusercontent.com/03bea5290b1f2b55fcf99ea58a983308d8031bd9a4594d6a7d5d34e4e8c62f47/68747470733a2f2f706f7365722e707567782e6f72672f70687077617463682f706870746f6b656e2d706f6c7966696c6c2f762f737461626c65)](https://packagist.org/packages/phpwatch/phptoken-polyfill) [![License](https://camo.githubusercontent.com/203f4ed0c22d03241e0610ce4c44f616b0826f28b1ed4faee6637bc9a9b51233/68747470733a2f2f706f7365722e707567782e6f72672f70687077617463682f706870746f6b656e2d706f6c7966696c6c2f6c6963656e7365)](https://github.com/phpwatch/phptoken-polyfill) [![CI](https://github.com/phpwatch/phptoken-polyfill/workflows/CI/badge.svg)](https://github.com/phpwatch/phptoken-polyfill/actions)

Synopsis
--------

[](#synopsis)

This library provides a compatible layer to bring `PhpToken` class that comes with PHP 8. It provides the full functionality of the class, but from user-land PHP code. Note that PHP 8's built-in `PhpToken` class is fast and memory-friendly because it is in C language. However, if you have a library that requires the `PhpToken` class, you can use this library to transparently bring that functionality to any PHP version 7.1 or later.

Prerequisites
-------------

[](#prerequisites)

- PHP 7.1 or later.
- Tokenizer extension (almost always bundled)

Installing
----------

[](#installing)

The simplest way would be to install using [composer](https://getcomposer.org).

```
composer require phpwatch/phptoken-polyfill
```

If the `PhpToken` class is not available in your system, composer autoloader will seamlessly autoload the class provided by this library to provide the same functionality.

If you cannot use Composer, try convincing whoever made that decision it is not 2012 anymore. If that doesn't work, you can manually load the classes in the `src/` directory.

Usage
-----

[](#usage)

Usage is exactly the same as PHP 8 native `PhpToken` class.

Make sure that the file is included. If you use Composer, include its autoload file. If the `PhpToken` class is natively available, this library will not be loaded at all. If you do not use Composer autoloader, you will need to manually `require` the files in the `src` directory.

Here is an example of using `PhpToken` class. The example below should work in any PHP version from 7.1 to 8.0 and later, even the `PhpToken` is not natively available.

```
$snippet = '';
$tokens = \PhpToken::tokenize($snippet);
```

Development, tests, and contributing
------------------------------------

[](#development-tests-and-contributing)

Contributions are welcome. Please open an issue or send a pull-request. Please make sure to run the tests in both Linux-based platforms and Windows. Windows uses CRLF line endings, which can make tests fail if you hardcode the assertions to expect a specific position within an LF/CR-preferred platform.

Please note that tests and other deveopment dependencies are not included when you download the zip files from Github. This is to keep the library size small. You need to clone the repository or fork it to get the full source.

Credits
-------

[](#credits)

[@Ayesh](https://github.com/Ayesh): [Ayesh Karunaratne](https://ayesh.me).

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~107 days

Total

3

Last Release

2013d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1703f88c1f225e3fadff28458e81328f86668d9631a8c489f45ea1aaebe12df2?d=identicon)[Ayesh](/maintainers/Ayesh)

---

Top Contributors

[![Ayesh](https://avatars.githubusercontent.com/u/811553?v=4)](https://github.com/Ayesh "Ayesh (50 commits)")[![spekulatius](https://avatars.githubusercontent.com/u/8433587?v=4)](https://github.com/spekulatius "spekulatius (1 commits)")

---

Tags

phppolyfillpolyfilltokenizerphp8

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/phpwatch-phptoken-polyfill/health.svg)

```
[![Health](https://phpackages.com/badges/phpwatch-phptoken-polyfill/health.svg)](https://phpackages.com/packages/phpwatch-phptoken-polyfill)
```

###  Alternatives

[symfony/polyfill-mbstring

Symfony polyfill for the Mbstring extension

7.8k1.2B515](/packages/symfony-polyfill-mbstring)[paragonie/random_compat

PHP 5.x polyfill for random\_bytes() and random\_int() from PHP 7

8.2k655.0M405](/packages/paragonie-random-compat)[symfony/polyfill-ctype

Symfony polyfill for ctype functions

4.0k982.0M125](/packages/symfony-polyfill-ctype)[symfony/polyfill-php72

Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions

4.8k674.7M31](/packages/symfony-polyfill-php72)[symfony/polyfill-intl-idn

Symfony polyfill for intl's idn\_to\_ascii and idn\_to\_utf8 functions

3.4k774.6M90](/packages/symfony-polyfill-intl-idn)[symfony/polyfill-intl-normalizer

Symfony polyfill for intl's Normalizer class and related functions

2.1k830.2M36](/packages/symfony-polyfill-intl-normalizer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
