{"id":16721154,"url":"https://github.com/khalyomede/prototype","last_synced_at":"2025-03-15T12:23:24.050Z","repository":{"id":57006176,"uuid":"122830476","full_name":"khalyomede/prototype","owner":"khalyomede","description":"Enable adding method to a class on the fly","archived":false,"fork":false,"pushed_at":"2018-03-28T22:40:13.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T03:47:38.789Z","etag":null,"topics":["class","extend","php","prototype"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/khalyomede/prototype","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khalyomede.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-25T11:36:19.000Z","updated_at":"2018-03-28T22:40:15.000Z","dependencies_parsed_at":"2022-08-21T14:30:52.625Z","dependency_job_id":null,"html_url":"https://github.com/khalyomede/prototype","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fprototype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fprototype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fprototype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Fprototype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khalyomede","download_url":"https://codeload.github.com/khalyomede/prototype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243727286,"owners_count":20337964,"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","extend","php","prototype"],"created_at":"2024-10-12T22:29:06.159Z","updated_at":"2025-03-15T12:23:24.011Z","avatar_url":"https://github.com/khalyomede.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prototype\n\n![PHP from Packagist](https://img.shields.io/packagist/php-v/khalyomede/prototype.svg)\n![Packagist](https://img.shields.io/packagist/v/khalyomede/prototype.svg)\n![Packagist](https://img.shields.io/packagist/l/khalyomede/prototype.svg)\n\n\nEnable adding method to a class on the fly.\n\n```php\nclass Tableau extends Prototype {\n  protected $items;\n\n  public function __construct($items = []) {\n    $this-\u003eitems = $items;\n  }\n}\n```\n\n```php\n$tableau = new Tableau(['php', 'python', 'nodejs']);\n\n$tableau-\u003eprototype('all', function() {\n  return $this-\u003eitems;\n});\n\n$languages = $tableau-\u003eall();\n```\n\n## Summary\n- [Prerequisites](#prerequisites)\n- [Installation](#installation)\n- [Examples of uses](#examples-of-uses)\n- [Methods definition](#methods-definition)\n- [MIT licence](#mit-licence)\n\n## Prerequisites\n\nThis project is bounded to the following requirements you should be aware of:\n- PHP \u003e= 7.0.0\n- Each class extending `Prototype` should not define the following method: `prototype()`\n- Each class extending `Prototype` should not define the following static method: `prototypes()`\n\n## Installation\n\nOn your project folder, open a command prompt and type: \n```bash\ncomposer require khalyomede/prototype:1.*\n```\n\n## Examples of uses\n\nAll these examples can be seen in `/example` folder.\n\n- [Example 1: extending a class with a simple function](#example-1-extending-a-class-with-a-simple-function)\n- [Example 2: extending a class with a simple method](#example-2-extending-a-class-with-a-simple-method)\n- [Example 3: extending a class with a method with parameters](#example-3-extending-a-class-with-a-method-with-parameters)\n\nFor all these examples, we will assume we have a function called `Tableau` that simulate the behavior of PHP collections:\n\n```php\nnamespace Me;\n\nuse Khalyomede\\Prototype;\n\nclass Tableau extends Prototype {\n  protected $items;\n\n  public function __construct($items = []) {\n    $this-\u003eitems = $items;\n  }\n\n  public function all() {\n    return $this-\u003eitems;\n  }\n}\n```\n\n### Example 1: extending a class with a simple function\n\n```php\nuse Me\\Tableau;\n\n$languages = ['php' =\u003e '7.2', 'python' =\u003e '3.6', 'nodejs' =\u003e '8.6'];\n\n$tableau = new Tableau($languages);\n\n$tableau-\u003eprototype('className', function() {\n  return 'Tableau';\n});\n\necho $tableau-\u003eclassName();\n```\n\nWill display:\n\n```bash\nTableau\n```\n\n### Example 2: extending a class with a simple method\n\n```php\nuse Me\\Tableau;\n\n$languages = ['php' =\u003e '7.2', 'python' =\u003e '3.6', 'nodejs' =\u003e '8.6'];\n\n$tableau = new Tableau($languages);\n\n$tableau-\u003eprototype('first', function() {\n  return $this-\u003eitems[key($this-\u003eitems)];\n});\n\n$version = $tableau-\u003efirst();\n\necho $version;\n```\n\nWill print:\n\n```bash\n7.2\n```\n\n### Example 3: extending a class with a method with parameters\n\n```php\nuse Me\\Tableau;\n\n$languages = ['php' =\u003e '7.2', 'python' =\u003e '3.6', 'nodejs' =\u003e '8.6'];\n\n$tableau = new Tableau($languages);\n\n$tableau-\u003eprototype('find', function($key) {\n  return isset($this-\u003eitems[$key]) ? $this-\u003eitems[$key] : null;\n});\n\n$python_version = $tableau-\u003efind('python');\n\necho $python_version;\n```\n\nWill echo:\n\n```bash\n3.6\n```\n\n## Methods definition\n\n- [prototypes()](#prototypes)\n- [prototype()](#prototype)\n\n### Prototypes()\n\nList all the registered prototypes. The list is shared across all the instance of your prototyped object.\n\n```php\npublic static function prototypes(): array\n```\n\n### Prototype()\n\nRegister a new method for the object. This method can then be accessed across all your instances of the prototyped object.\n\n```php\npublic function prototype(string $name, callable $function): Prototype\n```\n\n**Exceptions**\n\n`InvalidArgumentException`: \n\n- If the name of the function is already used by the prototyped object\n- If the prototype has already been registered for this object\n\n`BadMethodCallException`:\n\n- If the method (you thought you prototyped) has not been registered yet\n\n**Note**\n\nThis function returns an instance of the current object.\n\n## MIT licence\n\nPrototype\n\nCopyright © 2018 Khalyomede\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Fprototype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhalyomede%2Fprototype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Fprototype/lists"}