{"id":15038181,"url":"https://github.com/nikic/scalar_objects","last_synced_at":"2025-05-16T04:04:56.161Z","repository":{"id":6576406,"uuid":"7818503","full_name":"nikic/scalar_objects","owner":"nikic","description":"Extension that adds support for method calls on primitive types in PHP","archived":false,"fork":false,"pushed_at":"2024-07-21T04:26:42.000Z","size":138,"stargazers_count":1134,"open_issues_count":12,"forks_count":44,"subscribers_count":54,"default_branch":"master","last_synced_at":"2025-04-08T14:08:41.532Z","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/nikic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-01-25T10:51:22.000Z","updated_at":"2025-04-01T05:34:08.000Z","dependencies_parsed_at":"2024-12-05T20:02:29.185Z","dependency_job_id":"b835776a-8b3b-45a9-989e-4caed2b25c74","html_url":"https://github.com/nikic/scalar_objects","commit_stats":{"total_commits":99,"total_committers":11,"mean_commits":9.0,"dds":0.303030303030303,"last_synced_commit":"2215966ee0bf550f58c16091724d5e7a03df7d2a"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fscalar_objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fscalar_objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fscalar_objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fscalar_objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikic","download_url":"https://codeload.github.com/nikic/scalar_objects/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254464895,"owners_count":22075570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":"2024-09-24T20:37:27.637Z","updated_at":"2025-05-16T04:04:51.146Z","avatar_url":"https://github.com/nikic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Add support for method calls on primitive types in PHP\n=====================================================\n\nThis extension implements the ability to register a class that handles the\nmethod calls to a certain primitive type (string, array, ...). As such it\nallows implementing APIs like `$str-\u003elength()`.\n\nThe main purpose of this repo is to provide a proof of concept implementation\nthat can be used to design the new APIs. The switch to object syntax for\noperations on primitive types is a unique opportunity for PHP to redesign many\nof its inconsistent core APIs. This repo provides the means to quickly\nprototype and test new APIs as userland code. Once the APIs are figured out\nit will be proposed for inclusion into PHP.\n\nNote: The ability to register type handlers from userland is just for\nprototyping. It's not something I would actually want in PHP in the end.\n\nRegistering type handlers\n-------------------------\n\nType handlers are registered through `register_primitive_type_handler`. The\nfunction takes a type name (like \"string\" or \"array\") and a class name. The\nclass should contain static methods, which receive the primitive type as the\nfirst parameter:\n\n```php\n\u003c?php\n\nclass StringHandler {\n    public static function length($self) {\n        return strlen($self);\n    }\n\n    public static function startsWith($self, $other) {\n        return strpos($self, $other) === 0;\n    }\n}\n\nregister_primitive_type_handler('string', 'StringHandler');\n\n$string = \"abc\";\nvar_dump($string-\u003elength()); // int(3)\nvar_dump($string-\u003estartsWith(\"a\")); // bool(true)\n```\n\nThe valid type names are: `null`, `bool`, `int`, `float`, `string`, `array`\nand `resource`. Not all of those will make sense in practice, but for now they\nare all supported.\n\nImplemented APIs\n----------------\n\nAs already pointed out in the introduction the main purpose of this repo is\ndesigning good APIs for the primitive types. The implemented APIs are available\nin the `handlers/` folder (and are obviously work in progress). In order to\nload these APIs just include the `handlers/bootstrap.php` file.\n\nInstallation\n------------\n\nThe master branch supports PHP version 7.0 to 8.1. The extension is incompatible\nwith the JIT compiler.\n\n### Unix\n\nIn order to compile and install the extension run the following commands:\n\n    phpize\n    ./configure\n    make\n    sudo make install\n\n### Windows\n\nDownload a [prebuilt Windows DLL][windows_dlls] that matches your PHP version\nand move it into the `ext/` directory of your PHP installation. Furthermore\nyou'll have to add `extension=php_scalar_objects.dll` to your `php.ini`.\n\nTesting the extension\n---------------------\n\nThe extension comes with a `run-tests.php` file to run the tests. (To see\nexamples of the implemented APIs you should also look in the tests.) The\nscript is run as follows:\n\n    php run-tests.php -q -p php\n\nWhere `php` is the path to your PHP executable.\n\nLimitations\n-----------\n\nThis extension has a number of limitations:\n\n * Due to technical limitations, it is not possible to create *mutable* APIs for primitive\n   types. Modifying `$self` within the methods is not possible (or rather, will have no effect,\n   as you'd just be changing a copy).\n\n  [windows_dlls]: http://windows.php.net/downloads/pecl/snaps/scalar_objects/20170414/\n  [version_0_2]: https://github.com/nikic/scalar_objects/tree/0.2\n  [version_0_1]: https://github.com/nikic/scalar_objects/tree/0.1\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fscalar_objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikic%2Fscalar_objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fscalar_objects/lists"}