Prototype is a runtime extension library built for plugin development.
Initially inspired by JavaScript's .prototype
, but extended to allow for instance extensions.
- PHP5.4
You have three abstract classes at your disposal:
Prototype
— the main runtime extension component, used to extend classes with public properties and methods.Extendable
(extendsPrototype
) — the main instance level runtime extension component, used to register class based extensions that will be auto-applied to every instance.Extension
— not exactly for extensions, acts as an interface. Used in conjuction withExtendable
.
Prototype
:
<?php
use Prototype\Prototype as Proto;
// Simply extend with Prototype.
class A extends Proto
{}
// Create instance.
$a = new A;
// Add property.
$a->dummyProperty = 'x';
// Add method.
$a->dummyMethod = function()
{
// Yes, $this actually refers to the classes instance.
echo $this->dummyProperty;
// Chaining is also possible.
return $this;
};
// Test it.
$a->dummyMethod()->dummyMethod(); // xx
- Finish Usage:
Extendable
&Extension
.