PHPackages                             khalyomede/prototype - 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. khalyomede/prototype

ActiveLibrary

khalyomede/prototype
====================

Enable adding method to a class on the fly

v1.1.1(8y ago)0392MITPHPPHP &gt;=7.0.0

Since Feb 25Pushed 8y ago1 watchersCompare

[ Source](https://github.com/khalyomede/prototype)[ Packagist](https://packagist.org/packages/khalyomede/prototype)[ RSS](/packages/khalyomede-prototype/feed)WikiDiscussions master Synced 2d ago

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

Prototype
=========

[](#prototype)

[![PHP from Packagist](https://camo.githubusercontent.com/569adfb037dd172e0c4f2ca543cffb386d22eeaf5dac1bbef13762645bb7b2ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6b68616c796f6d6564652f70726f746f747970652e737667)](https://camo.githubusercontent.com/569adfb037dd172e0c4f2ca543cffb386d22eeaf5dac1bbef13762645bb7b2ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6b68616c796f6d6564652f70726f746f747970652e737667)[![Packagist](https://camo.githubusercontent.com/d332f830437f085eeb666a9d49942527871d316dd50cc4bae40693a9649294d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b68616c796f6d6564652f70726f746f747970652e737667)](https://camo.githubusercontent.com/d332f830437f085eeb666a9d49942527871d316dd50cc4bae40693a9649294d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b68616c796f6d6564652f70726f746f747970652e737667)[![Packagist](https://camo.githubusercontent.com/d632e72702394368c31b9dbbdabfb5fa1f7c2aee3ce584a2cd02323571a9ac42/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b68616c796f6d6564652f70726f746f747970652e737667)](https://camo.githubusercontent.com/d632e72702394368c31b9dbbdabfb5fa1f7c2aee3ce584a2cd02323571a9ac42/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b68616c796f6d6564652f70726f746f747970652e737667)

Enable adding method to a class on the fly.

```
class Tableau extends Prototype {
  protected $items;

  public function __construct($items = []) {
    $this->items = $items;
  }
}
```

```
$tableau = new Tableau(['php', 'python', 'nodejs']);

$tableau->prototype('all', function() {
  return $this->items;
});

$languages = $tableau->all();
```

Summary
-------

[](#summary)

- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Examples of uses](#examples-of-uses)
- [Methods definition](#methods-definition)
- [MIT licence](#mit-licence)

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

[](#prerequisites)

This project is bounded to the following requirements you should be aware of:

- PHP &gt;= 7.0.0
- Each class extending `Prototype` should not define the following method: `prototype()`
- Each class extending `Prototype` should not define the following static method: `prototypes()`

Installation
------------

[](#installation)

On your project folder, open a command prompt and type:

```
composer require khalyomede/prototype:1.*
```

Examples of uses
----------------

[](#examples-of-uses)

All these examples can be seen in `/example` folder.

- [Example 1: extending a class with a simple function](#example-1-extending-a-class-with-a-simple-function)
- [Example 2: extending a class with a simple method](#example-2-extending-a-class-with-a-simple-method)
- [Example 3: extending a class with a method with parameters](#example-3-extending-a-class-with-a-method-with-parameters)

For all these examples, we will assume we have a function called `Tableau` that simulate the behavior of PHP collections:

```
namespace Me;

use Khalyomede\Prototype;

class Tableau extends Prototype {
  protected $items;

  public function __construct($items = []) {
    $this->items = $items;
  }

  public function all() {
    return $this->items;
  }
}
```

### Example 1: extending a class with a simple function

[](#example-1-extending-a-class-with-a-simple-function)

```
use Me\Tableau;

$languages = ['php' => '7.2', 'python' => '3.6', 'nodejs' => '8.6'];

$tableau = new Tableau($languages);

$tableau->prototype('className', function() {
  return 'Tableau';
});

echo $tableau->className();
```

Will display:

```
Tableau
```

### Example 2: extending a class with a simple method

[](#example-2-extending-a-class-with-a-simple-method)

```
use Me\Tableau;

$languages = ['php' => '7.2', 'python' => '3.6', 'nodejs' => '8.6'];

$tableau = new Tableau($languages);

$tableau->prototype('first', function() {
  return $this->items[key($this->items)];
});

$version = $tableau->first();

echo $version;
```

Will print:

```
7.2
```

### Example 3: extending a class with a method with parameters

[](#example-3-extending-a-class-with-a-method-with-parameters)

```
use Me\Tableau;

$languages = ['php' => '7.2', 'python' => '3.6', 'nodejs' => '8.6'];

$tableau = new Tableau($languages);

$tableau->prototype('find', function($key) {
  return isset($this->items[$key]) ? $this->items[$key] : null;
});

$python_version = $tableau->find('python');

echo $python_version;
```

Will echo:

```
3.6
```

Methods definition
------------------

[](#methods-definition)

- [prototypes()](#prototypes)
- [prototype()](#prototype)

### Prototypes()

[](#prototypes)

List all the registered prototypes. The list is shared across all the instance of your prototyped object.

```
public static function prototypes(): array
```

### Prototype()

[](#prototype-1)

Register a new method for the object. This method can then be accessed across all your instances of the prototyped object.

```
public function prototype(string $name, callable $function): Prototype
```

**Exceptions**

`InvalidArgumentException`:

- If the name of the function is already used by the prototyped object
- If the prototype has already been registered for this object

`BadMethodCallException`:

- If the method (you thought you prototyped) has not been registered yet

**Note**

This function returns an instance of the current object.

MIT licence
-----------

[](#mit-licence)

Prototype

Copyright © 2018 Khalyomede

Permission 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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE 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.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~5 days

Total

3

Last Release

2989d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15908747?v=4)[Anwar](/maintainers/khalyomede)[@khalyomede](https://github.com/khalyomede)

---

Top Contributors

[![khalyomede](https://avatars.githubusercontent.com/u/15908747?v=4)](https://github.com/khalyomede "khalyomede (22 commits)")

---

Tags

classextendphpprototype

### Embed Badge

![Health badge](/badges/khalyomede-prototype/health.svg)

```
[![Health](https://phpackages.com/badges/khalyomede-prototype/health.svg)](https://phpackages.com/packages/khalyomede-prototype)
```

PHPackages © 2026

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