Download the PHP package alpa/tools_proxy_object without Composer
On this page you can find all versions of the php package alpa/tools_proxy_object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alpa/tools_proxy_object
More information about alpa/tools_proxy_object
Files in alpa/tools_proxy_object
Package tools_proxy_object
Short Description Creates a proxy object and assigns getter / setter handlers to the object's members when accessing them.
License MIT
Informations about the package tools_proxy_object
Dev work plan
install
composer require alpa/tools_proxy_object:1.0.14
Changes
- v1.0.12 Added the ability to return the result by reference (For actions: get,call,invoke) - Warn: No backward compatibility
- v1.0.13 Added check for property initialization before passing by reference- compatible with prev version
- v1.0.14 Bugs fixed error :Only variable references should be retuned (or assigned) by reference . - Warn: No backward compatibility .
Closures for get action, call action, invoke action now needs to be declared by reference (
function & (){}
), and the result returned via a variable. There is now an abstract class for creating new proxies. Interfaces were created for the purpose of dependency inversion.
Description
A proxy object is an object that implements all the available magic methods that run the corresponding handlers. These handlers perform a number of custom actions on the target object.
Basically, this behavior is necessary for processing the result when accessing properties or methods of the target object.
In simple words:
- before getting / writing the value of the property of the target object, it must be processed in the sandbox.
- before executing the target object method, it needs to be processed in the sandbox.
The component creates a proxy object for the observed object or class.
Action handlers (get | set | call | isset | unset | invoke | toString | iterator) are assigned for each member of the observable object or class .
A similar principle is implemented in javascript through the Proxy constructor.
When accessing a member of an object, through the proxy object, the assigned handler for the specific action will be invoked.
Where the component can be applied:
- mediator for data validation;
- access to private data of an object through reflection;
- dynamic data formation, and generation of other properties;
- dynamic data requests, for example from a database;
- other options.
Note
1
From version 1.0.0 to version 1.1.0 is experimental development, where the API will be subject to modification.
See the tag description on GitHub for version compatibility.
When including a component in a project, specify a specific version.
The description of the api for a specific version can be found in the commit version.
2
If for some reason you have bugs or other problems, then it is recommended to implement your own handlers classes that fix this problem. See - Creating handler classes
3
The proxy object is a wrapper object that implements magic methods. By default it is not possible to access protected / private members of an observable object / class. The default proxy only works with the public members of the observable / class. If you need the ability to work with protected / private members of an object / class, use Reflector classes in action handlers.
4
Not the fact that the use of proxy objects will be fast. But in any case, this is the optimal solution for creating complex combinations and in a simple way.
Install
composer require alpa/tools_proxy_object
Getting started
example 1:
example 2:
example 3:
example 4 -
Definitions
Definitions:
- member(s) - properties and methods of an object or class
- action (s) -Actions that can be applied to members of a class or object(
set|get|isset|unset|call
). As well actions that are applied to a object or class (invoke | toString |iterator
). Sometimes the definition of "action" is understood as an action handler. - handler(s) or action(s) handlers - A function or method that handles actions
- proxy - an object with declared magic methods, which will pass actions through itself to the members of the observable object or class. The proxy object is a wrapper object that implements magic methods.The default proxy only works with the public members of the observable / class.
Create handlers for Proxy object
There are two ways to write handlers:
- dynamic writing of handlers through closure functions.
- writing of handlers through class declaration.
There are two types of handlers:
- a handler for a specific member of an object;
- handler for all members of the object;
If action handler is no assigned to a member, then an action handler for all members is applied.
If action handler is no assigned to members, then standard actions will be applied.
The following actions exist when accessing the members of an object:
- set - member value entry;
- get - member value query;
- isset - member check ;
- unset - member delete;
- call - member call;
- invoke - invoke object or class;
- toString - converting object or class to string;
- iterator - assigning an iterator when iterating over the members of an object.
Dynamic writing of handlers through closure functions
Example in the constructor
Handlers can be assigned outside of the constructor.
An example of assigning handlers via the Handlers :: init method
An example of assigning handlers for a specific property
or
Static writing of handlers through class declaration.
Class declaration in which methods will be handlers.
or
or
You can declare the following instance methods as handlers (when inheriting classes Alpa\ProxyObject\Handlers\StaticActions
or Alpa\ProxyObject\Handlers\InstanceActions
or Alpa\ProxyObject\Handlers\Instance
) :
- get - member value query;
- get_{$name_property} - value query of a member named $name_property;
- set - member value entry;
- set_{$name_property} - value entry of a member named $name_property;
- isset - checking is set member;
- isset_{$name_property} - checking is set a member named $name_property;
- unset - delete a member;
- unset_{$name_property} - removing a member named $name_property;
- call - call member;
- call_{$name_property} - call a member named $name_property;
- invoke - invoke object or class;
- toString - converting object or class to string;
- iterator - assigning an iterator to foreach;
You can declare the following static methods as handlers : (when inheriting class Alpa\ProxyObject\Handlers\Instance
)
- static_get - member value query;
- staticget{$name_property} - value query of a member named $name_property;
- static_set - member value entry;
- staticset{$name_property} - value entry of a member named $name_property;
- static_isset - checking is set member;
- staticisset{$name_property} - checking is set a member named $name_property;
- static_unset - delete a member;
- staticunset{$name_property} - removing a member named $name_property;
- static_call - call member;
- staticcall{$name_property} - call a member named $name_property;
- static_invoke - invoke object or class;
- static_toString - converting object or class to string;
- static_iterator - assigning an iterator to foreach;
A templates for creating action handlers for all members of an object.
Template where static actions and actions for an instance are declared
Template where only instance actions
Template where only static actions
Action handlers for a specific member are created similar to action handlers for all properties. The exceptions are the "invoke"? "toString" and "iterator" actions. they only apply to the observable object or class.
Example:
Proxying class static members
In addition to proxying the members of an object, it is possible to proxy static members of a class. To do this, instead of an object, you will need to specify the class
Creating handler classes
The constructor of the Alpa \ ProxyObject \ Proxy
class can accept as handlers any object or class that implements the Alpa \ ProxyObject \ Handlers \ ActionsInterface
interface.
For each action (set | get | isset | unset | call | invoke | toString | iterator) you will need to implement working code.
If for some reason you have bugs or other problems, then it is recommended to implement your own handlers classes that fix this problem.
Other Proxy Class
(v>=1.0.11) When creating a proxy object by handlers, you can specify which proxy class to use
Difficulties
For a member of a proxy object, it doesn't make sense to apply checks such as property_exists
ormethod_exists
or similar, since they will be applied directly to the proxy object. Therefore, when working with a proxy, always use the isset
check. If you have complex logic where you need to check both properties and methods, then it is recommended to separate the logic for working with properties and the logic for working with methods.