{"id":23302406,"url":"https://github.com/skrtdev/prototypes","last_synced_at":"2025-08-02T14:06:39.988Z","repository":{"id":49972002,"uuid":"330447085","full_name":"skrtdev/prototypes","owner":"skrtdev","description":"Dinamically add methods to PHP classes","archived":false,"fork":false,"pushed_at":"2021-06-28T10:31:57.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-13T04:45:36.915Z","etag":null,"topics":["class","hacktoberfest","methods","php","prototypes"],"latest_commit_sha":null,"homepage":"","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/skrtdev.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}},"created_at":"2021-01-17T17:28:04.000Z","updated_at":"2021-10-01T11:55:07.000Z","dependencies_parsed_at":"2022-09-09T22:41:33.557Z","dependency_job_id":null,"html_url":"https://github.com/skrtdev/prototypes","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skrtdev%2Fprototypes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skrtdev%2Fprototypes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skrtdev%2Fprototypes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skrtdev%2Fprototypes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skrtdev","download_url":"https://codeload.github.com/skrtdev/prototypes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247563421,"owners_count":20958967,"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":["class","hacktoberfest","methods","php","prototypes"],"created_at":"2024-12-20T10:29:03.753Z","updated_at":"2025-04-06T22:29:49.968Z","avatar_url":"https://github.com/skrtdev.png","language":"PHP","readme":"# Prototypes\n\u003e Dinamically add methods to PHP classes\n\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/skrtdev/prototypes/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/skrtdev/prototypes/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/skrtdev/prototypes/badges/build.png?b=master)](https://scrutinizer-ci.com/g/skrtdev/prototypes/build-status/master) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/ab3a826ec45e45d7bcc910c39df1331a)](https://www.codacy.com/gh/skrtdev/prototypes/dashboard?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=skrtdev/prototypes\u0026amp;utm_campaign=Badge_Grade) ![php version](https://img.shields.io/badge/php-%3E%3D7.4-blueviolet)  \nUsing this library you can dinamcally add methods to classes, as in the following example:  \n```php\nuse skrtdev\\Prototypes\\Prototypeable;\n\nclass MyClass{\n    use Prototypeable;\n}\n\nMyClass::addMethod('wow', function () {\n    return \"What a nice way to use PHP\";\n});\n\n$instance = new MyClass();\n\necho $instance-\u003ewow();\n```\nOutput is `What a nice way to use PHP`\n\n### Main Features\n\n  - Closures are bound to the original object, so you can access `$this` inside closures in the same way as you do when writing a normal method for that class.\n  - Supports **static** methods too, and you can access `self` and `static` too.  \n  - A native-like `\\Error` will be thrown when trying to call a non-existent method.  \n  - A `skrtdev\\Prototypes\\Exception` will be thrown if class method already exists, is a prototype, class does not exist or isn't Prototypeable (when using `skrtdev\\Prototypes\\Prototypes::addMethod()`).\n  - Ability to use any kind of `callable`s, not just `Closure`s.\n  - Ability to use [named arguments](https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments) in Prototypes methods.\n\n### Check if a Class is Prototypeable\n\nYou may need to know if a class is `Prototypeable` before trying to add methods to it.\n\nYou can use `isPrototypeable` method:  \n```php\nuse skrtdev\\Prototypes\\Prototypes;\n\nvar_dump(Prototypes::isPrototypeable($instance::class)); // you must pass the class name as string (use get_class() in php7.x)\nvar_dump(Prototypes::isPrototypeable(MyClass::class));\n```\n\n### Fun fact\n\nThe `Prototypes` class itself is `Prototypeable`, how strange.  \n\n### Known issues\n\n  - This library does not have `Inheritance`: you won't be able to use Prototypes methods defined for a class in his child classes. (this is going to be added soon)  \n  - You can't override already-prototyped methods, but this will be added soon.  \n  - Any kind of `Error/Exception`(s) look a bit strange in the Stack traces, and method name is hidden. Maybe there is a solution; if you find it, feel free to open an `Issue/Pull Request`.  \n\n### Future scope\n\n  - Use `class_parent()` to implement some kind of `Inheritance`. This may slow up calling prototypes in classes with a long hierarchy.  \n  - Maybe add the ability to add a method to a class without adding it to his children. (does it make sense?)\n  - Allow to add all methods of a normal/anonymous class into a Prototypeable one (Using `Reflection`?).  \n  - Add the ability to define prototype methods that has been already defined as prototypes, overwriting them.  \n  - Add the ability to define prototypes for all the Prototypeable classes. (do you see any use cases?)  \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskrtdev%2Fprototypes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskrtdev%2Fprototypes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskrtdev%2Fprototypes/lists"}