Download the PHP package icanboogie/prototype without Composer
On this page you can find all versions of the php package icanboogie/prototype. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download icanboogie/prototype
More information about icanboogie/prototype
Files in icanboogie/prototype
Package prototype
Short Description Implements the Prototype design pattern; getters/setters
License BSD-3-Clause
Homepage https://icanboogie.org/
Informations about the package prototype
Prototype
The icanboogie/prototype package allows methods of classes using the PrototypeTrait to be defined at runtime, and since the icanboogie/accessor package is used, this also includes getters and setters.
Installation
Defining methods at runtime
Methods can be defined at runtime using the prototype of a class. They are immediately available to every instance of the class and are inherited by the sub-classes of that class.
Defining getters and setters at runtime
Because getters and setters are methods too, they are defined just like regular methods.
Dependency injection, inversion of control
Dependency injection and inversion of control can be implemented using prototype lazy getters.
The following example demonstrates how a magic image
property can be defined to lazy load a
record from an ActiveRecord model.
Prototype methods and parent::
Prototype methods can be overridden and work with parent::
calls just like regular methods.
In the following example the prototype method url()
is added to the class Node
and
overridden in the class News
, still parent::
can be used from News
:
Defining prototypes methods
Prototype methods can be defined using a global configuration; through the prototype
property
of an Prototyped
instance; or using the Prototype
instance associated with classes using
the PrototypeTrait trait.
Defining prototypes methods using a global configuration
All prototypes can be configured using a single global configuration. For each class you can define the methods that the prototype implements.
The following example demonstrate how the meow()
method is defined for instances of the Cat
and FierceCat
classes. Although they are defined using closure in the example, methods can be
defined using any callable such as "App\Hooks::cat_meow"
.
Defining prototypes methods through the prototype
property
Prototype methods may be defined using the prototype
property:
Defining prototypes methods using a prototype instance
Prototype methods may be defined using the Prototype
instance of a class:
Defining prototypes methods using config fragments
If the package is bound to ICanBoogie using icanboogie/bind-prototype, prototype methods may be defined
using prototype
configuration fragments:
Getting an array representation of an object
An array representation of an Prototyped
instance can be obtained using the to_array()
method. Only
public and façade properties are exported.
As mentioned before, façade properties are also exported. The to_array()
method should be
overrode to alter this behavior.
Additionally the to_array_recursive()
method can be used to recursively convert an instance
into an array, in which case all the instances of the tree implementing ToArray
or ToArrayRecursive are converted into arrays.
Getting a JSON representation of an object
The to_json()
method can be used to get a JSON representation of an object. The method is
really straight forward, it invokes to_array_recursive()
and pass the result to
json_encode()
.
Creating an instance from an array of properties
The Prototyped::from()
method creates an instance from an array of properties:
Instances are created in the same fashion PDO
creates instances when fetching objects using the FETCH_CLASS
mode, that is the properties
of the instance are set before its constructor is invoked.
Exceptions
The following exceptions are defined:
- MethodNotDefined: Exception thrown in attempt to access a method that is not defined.
- MethodOutOfScope: Exception thrown in attempt to invoke a method that is out of scope.
Continuous Integration
The project is continuously tested by GitHub actions.
Code of Conduct
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.
Contributing
Please see CONTRIBUTING for details.
License
icanboogie/prototype is released under the BSD-3-Clause.