{"id":37009897,"url":"https://github.com/imliam/php-modifiers","last_synced_at":"2026-01-14T00:58:16.917Z","repository":{"id":56990408,"uuid":"182339244","full_name":"imliam/php-modifiers","owner":"imliam","description":"Adds the ability to apply a range of optional modifiers `! @ ~ + -` when calling your class methods to augment them with a unique syntax.","archived":false,"fork":false,"pushed_at":"2025-03-09T18:48:34.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-21T20:07:50.943Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imliam.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-20T00:01:50.000Z","updated_at":"2025-03-09T18:48:16.000Z","dependencies_parsed_at":"2022-08-21T10:10:52.557Z","dependency_job_id":null,"html_url":"https://github.com/imliam/php-modifiers","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/imliam/php-modifiers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Fphp-modifiers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Fphp-modifiers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Fphp-modifiers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Fphp-modifiers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imliam","download_url":"https://codeload.github.com/imliam/php-modifiers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Fphp-modifiers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28407623,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T00:40:43.272Z","status":"ssl_error","status_checked_at":"2026-01-14T00:40:42.636Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-14T00:56:24.900Z","updated_at":"2026-01-14T00:58:14.719Z","avatar_url":"https://github.com/imliam.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Modifiers\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/imliam/php-modifiers.svg)](https://packagist.org/packages/imliam/php-modifiers)\n![Code Quality](https://img.shields.io/scrutinizer/g/imliam/php-modifiers.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/imliam/php-modifiers.svg)](https://packagist.org/packages/imliam/php-modifiers)\n[![License](https://img.shields.io/github/license/imliam/php-modifiers.svg)](LICENSE.md)\n\nAdds the ability to apply a range of optional modifiers `! @ ~ + -` when calling your class methods to augment them with a unique syntax.\n\nFor example, we may have a class where each of the following may do different things:\n\n```php\nSlack::say('Hello world'); // 'Hello world'\n!Slack::say('Hello world'); // 'HELLO WORLD'\n@Slack::say('Hello world'); // 'Someone said \"Hello world\"'\n@!Slack::say('Hello world'); // 'Someone said \"HELLO WORLD\"'\n```\n\n\u003c!-- TOC --\u003e\n\n- [PHP Modifiers](#php-modifiers)\n  - [💾 Installation](#-installation)\n  - [📝 Usage](#-usage)\n    - [Global Functions](#global-functions)\n  - [The Modifiers](#the-modifiers)\n    - [`!` Exclamation Mark](#-exclamation-mark)\n    - [`+` Plus Symbol](#-plus-symbol)\n    - [`-` Minus Symbol](#--minus-symbol)\n    - [`~` Tilde Symbol](#-tilde-symbol)\n    - [`@` \"At\" Symbol](#-at-symbol)\n  - [How It Works](#how-it-works)\n  - [✅ Testing](#-testing)\n  - [🔖 Changelog](#-changelog)\n  - [⬆️ Upgrading](#️-upgrading)\n  - [🎉 Contributing](#-contributing)\n    - [🔒 Security](#-security)\n  - [👷 Credits](#-credits)\n  - [♻️ License](#️-license)\n\n\u003c!-- /TOC --\u003e\n\n## 💾 Installation\n\nYou can install the package with [Composer](https://getcomposer.org/) using the following command:\n\n```bash\ncomposer require imliam/php-modifiers:^1.0.0\n```\n\n## 📝 Usage\n\nThis package's functionality is exposed through the `HasModifiers` trait, which can be applied to a class:\n\n```php\nclass Example\n{\n    use HasModifiers;\n}\n```\n\nWith the trait applied to the class, you must also define the class methods that can have modifiers used. This can be done by setting a static `$modifierAliases` property on the class.\n\nThis property should be an array that contains pairs of the class and method name that can be used.\n\nFor example if we wanted the `say` method on the current class to be able to use modifiers:\n\n```php\npublic static $modifierAliases = [\n    [self::class, 'say'],\n];\n```\n\nWith this set up, inside the method we can now check if certain modifiers have been used by calling the `static::hasModifier()` method:\n\n```php\npublic static function say($string)\n{\n    if (static::hasModifier('!')) {\n        echo strtoupper($string);\n        return;\n    }\n\n    echo $string;\n}\n```\n\nWith it all in place, we can now call our method either with or without the modifier to get the desired behaviour:\n\n```php\nExample::say('Hello world'); // 'Hello world'\n!Example::say('Hello world'); // 'HELLO WORLD'\n```\n\n### Global Functions\n\nIf we wanted to register a global function that can accept modifiers, we can use one to call our class method, as well as registering it as an alias by adding it to the `$modifierAliases` property.\n\n```php\nfunction say($string)\n{\n    return Example::say($string);\n}\n\nExample::$modifierAliases[] = 'say';\n```\n\nThe function will now work in the same way as before:\n\n```php\nsay('Hello world'); // 'Hello world'\n!say('Hello world'); // 'HELLO WORLD'\n```\n\n## The Modifiers\n\nThere are five modifiers available to use. It is very important to note that as they are all also regular operators in PHP, each of them have their own quirks that might change the expected behaviour and return values of the methods.\n\nUsing them\n\nDue to this, it would make sense to only use these modifiers on void functions that are not expected to return anything of use.\n\n### `!` Exclamation Mark\n\n**Do not use the return value with this modifier.**\n\nThe exclamation mark is a [logical operator](https://www.php.net/manual/en/language.operators.logical.php) in PHP that negates the value to the opposite boolean. This means that any truthy value that is returned will be false, and any falsy value returned will be true.\n\n### `+` Plus Symbol\n\n**Do not use the return value with this modifier.**\n\nThe plus symbol is an identity [arithmetic operator](https://www.php.net/manual/en/language.operators.arithmetic.php) in PHP. Used as a modifier, it will attempt to cast the return value to an integer or float.\n\n### `-` Minus Symbol\n\n**Do not use the return value with this modifier.**\n\nThe plus symbol is a negative identity [arithmetic operator](https://www.php.net/manual/en/language.operators.arithmetic.php) in PHP. Used as a modifier, it will attempt to cast the return value to an integer or float.\n\n### `~` Tilde Symbol\n\n**Do not use the return value with this modifier.**\n\nThe tilde symbol is a [bitwise operator](https://www.php.net/manual/en/language.operators.bitwise.php) in PHP. However, as the operator itself performs a bitwise operation, any method using this operator **must return an integer value**, or something that can be cast to one.\n\n### `@` \"At\" Symbol\n\n**You can use the return value with this modifier.**\n\nThe \"at\" symbol is an [error suppression operator](https://www.php.net/manual/en/language.operators.errorcontrol.php) in PHP that hides and ignores any errors that occur in the proceeding statement - meaning errors that might be within your method will be unexpectedly ignored.\n\nHowever, this is the only one of the operators that does not alter the return value of the method.\n\n```php\npublic static function hasSomething()\n{\n    if (static::hasModifier('@')) {\n        trigger_error(\"An error should occur here, but it gets ignored…\");\n\n        return 'A modified value…';\n    }\n\n    return 'A non-modified value…';\n}\n\necho Example::hasSomething(); // 'A non-modified value…'\necho @Example::hasSomething(); // 'A modified value…'\n```\n\n## How It Works\n\n✨ Magic ✨\n\nThe modifier functionality works by taking a stack trace at the point the method was called, finding the point aliased method was called further up in the stack trace. Once these aliases are found, the file is read as a string and the [PHP source tokens are parsed](https://php.net/manual/en/function.token-get-all.php) to find the operators that came before it.\n\nThis was originally seen in the [Kint](https://github.com/kint-php/kint) package as a way to quickly augment how your variables are displayed while debugging. Because it was designed for this environment, great performance was not a huge concern. Following stack traces and parsing source code are generally _slow_ operations in PHP, so take it with a grain of salt and don't abuse it in large production applications.\n\nCheck the source code to see how it works in more depth. This package is a stripped down version of Kint's implementation that fits in a couple of small source files.\n\n## ✅ Testing\n\n``` bash\ncomposer test\n```\n\n## 🔖 Changelog\n\nPlease see [the changelog file](CHANGELOG.md) for more information on what has changed recently.\n\n## ⬆️ Upgrading\n\nPlease see the [upgrading file](UPGRADING.md) for details on upgrading from previous versions.\n\n## 🎉 Contributing\n\nPlease see the [contributing file](CONTRIBUTING.md) and [code of conduct](CODE_OF_CONDUCT.md) for details on contributing to the project.\n\n### 🔒 Security\n\nIf you discover any security related issues, please email liam@liamhammett.com instead of using the issue tracker.\n\n## 👷 Credits\n\n- [Liam Hammett](https://github.com/imliam)\n- [Kint](https://github.com/kint-php/kint) and all of its contributors for the implementation\n- [All Contributors](../../contributors)\n\n## ♻️ License\n\nThe MIT License (MIT). Please see the [license file](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimliam%2Fphp-modifiers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimliam%2Fphp-modifiers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimliam%2Fphp-modifiers/lists"}