Download the PHP package tasso/prototype without Composer
On this page you can find all versions of the php package tasso/prototype. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tasso/prototype
More information about tasso/prototype
Files in tasso/prototype
Package prototype
Short Description Simple prototype-based programming for PHP
License MIT
Homepage http://github.com/tassoevan/prototype
Informations about the package prototype
Prototype
Simple prototype-based programming in PHP
Prototype programming is a flavor of object-oriented programming that deals with objects without categorization provided by classes or interfaces. All objects can be identified and differentiated by duck typing. Basically, properties and methods are put in objects on runtime, instead of previously declared in classes; inheritance is obtained by object cloning and dynamic insertion of properties. Thus parent objects act as prototypes for their children.
This allow you to quickly code, once you do structural changes in your source code while you're programming. However, although good for small projects, it can generate unmaintainable applications. It's recommmended that you use prototyping to prototype application modules first and implement an equivalent traditional class-based structure after.
Installation
The recommended way to install this package is through Composer. Create a common
composer.json
file and run:
Documentation
Basic usage
All prototypes are instances of TassoEvan\Prototype\Prototype
class. Properties can be added and removed exactly like
the native stdClass
. The key difference between Prototype
and stdClass
is that the last one doesn't handle
callable arguments and closures well. You can call closures in a Prototype
instance exactly as they are methods.
Also, you can use Prototype
instances as closures too:
Property wrappers
All properties assigned to Prototype
instances are wrapped in instances of abstract class
TassoEvan\Prototype\Property
. It can be done explicitly instantiating these objects or calling static methods from
TassoEvan\Prototype\Prototype
, implicitly wrapping normal properties by simple assignment though.
Explicitly, instanciating TassoEvan\Prototype\Property
:
Explicitly, via static methods of TassoEvan\Prototype\Prototype
:
Implicitly (TassoEvan\Prototype\NormalProperty
only):
TassoEvan\Prototype\NormalProperty
A normal property can be assigned, evaluated and called (if it is callable) as any variable or object public property in PHP.
TassoEvan\Prototype\ReadOnlyProperty
A read only property can't be overwritten. In strict mode, a exception will be thrown when a new assignment is done.
TassoEvan\Prototype\LazyProperty
A lazy property has the ability to be lazy-loaded: at first try to get a value (or call it), a callable named loader is invoked to generate the property value, that will be stored. After loaded, the property behaves like a normal property.
TassoEvan\Prototype\ProxyProperty
TassoEvan\Prototype\DynamicProperty
A dynamic property doesn't have a stored value: you should define a getter and a setter callables to offer some value. Can be useful to represent or be a facade to service providers and containers.
Additional features
Prototype::closure($callable)
: transforms any callable in a closure that can be passed toPrototype
instancesPrototype::data(Prototype $obj)
: creates an array with all data that your prototype stores or generates: that means thatPrototype::dynamic()
andPrototype::lazy()
calls will be performed. This static method was implemented attending the fact that prototypes can't be serialized, once they store closures. Further it can be reviewed.
Contributions
You are free to fork and contribute to this project.