Download the PHP package northwoods/entity-proxy without Composer
On this page you can find all versions of the php package northwoods/entity-proxy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download northwoods/entity-proxy
More information about northwoods/entity-proxy
Files in northwoods/entity-proxy
Package entity-proxy
Short Description A hydrating proxy for objects with private properties
License MIT
Informations about the package entity-proxy
Northwoods Entity Proxy
A reflection based proxy for hydrating objects with private properties.
Attempts to be PSR-1, PSR-2, and PSR-4 compliant.
Install
Usage
Proxies are created with the singleton ProxyFactory
:
Proxies can be created as wrappers around existing objects:
Or new instances when hydrating from other data sources:
Proxies can also read existing values on the object:
Reasoning
In Domain Driven Design it is often recommended that entities use private properties with getters to access state and setters to change state based on business requirements. Since an entity is considered to be a persistent object, its constructor should only be called once for the lifetime of the entity. This allows for domain events to be triggered by the constructor to notify the application that, for example, a new user has registered.
With this limitation in mind, the requirements for the hydrator are:
- It must not call the entity constructor.
- It must be able to set private/protected properties.
- It must be as efficient as possible.
The easiest way to achieve these goals is to use reflection, which allows us to create objects without constuctor, make properties accessible, and write property values. The reflection of every class should be internally cached for the lifetime of the factory to maximize performance.
If performance is of concern there are other viable approaches that are faster but more complicated to setup and use.
License
MIT