1. Go to this page and download the library: Download alpa/tools_proxy_object library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alpa / tools_proxy_object example snippets
use Alpa\Tools\ProxyObject\ProxyInterface;
use Alpa\Tools\ProxyObject\Handlers;
class MyHandlers extends Handlers\Instance
{
protected static function & static_get ( $target,string $prop,$val_or_args,ProxyInterface $proxy)
{
$answer=is_string($target->$prop) ? strtoupper($target->$prop) : $target->$prop;
return $answer; //Only variables are returned by reference
}
protected static function & static_get_test( $target,string $prop,$val_or_args,ProxyInterface $proxy)
{
$answer = is_string($target->$prop) ? strtolower($target->$prop) : $target->$prop;
return $answer ; //Only variables are returned by reference
}
}
$obj=(object)[
'test'=>'HELLO',
'other'=>'bay'
];
$proxy=new Proxy($obj,MyHandlers::class);
echo $proxy->test; // hello
echo $proxy->other;// BAY
use Alpa\Tools\ProxyObject\Proxy;
use Alpa\Tools\ProxyObject\ProxyInterface;
use Alpa\Tools\ProxyObject\Handlers;
class MyHandlers extends Handlers\Instance
{
public function __construct($prefix)
{
$this->prefix=$prefix;
}
protected function & get( $target,string $prop,$val_or_args,ProxyInterface $proxy)
{
$answer = is_string($target->$prop) ? strtoupper($this->prefix.$target->$prop) : $target->$prop;
return $answer; //Only variables are returned by reference
}
protected function & get_test($target,string $prop,$val_or_args,ProxyInterface $proxy)
{
$answer = is_string($target->$prop) ? strtolower($this->prefix.$target->$prop) : $target->$prop;
return $answer; //Only variables are returned by reference
}
}
$inst=new MyHandlers('Alex ');
$obj=(object)[
'test'=>'HELLO',
'other'=>'bay'
];
$proxy=new Proxy($obj,$inst);
//or $proxy=$inst::proxy($obj,$inst);
// or $proxy=new Proxy($obj,$inst);
echo $proxy->test; // alex hello
echo $proxy->other;// ALEX BAY
use Alpa\Tools\ProxyObject\Proxy;
use Alpa\Tools\ProxyObject\ProxyInterface;
use Alpa\Tools\ProxyObject\Handlers;
$handlers = new Handlers\Closures();
$handlers->init('get',function & ($target,$prop,ProxyInterface $proxy){
$answer= is_string($target->$prop) ? strtoupper($target->$prop) : $target->$prop;
return $answer; //Only variables are returned by reference
});
$handlers->initProp('get','test',function & ($target,$prop, ProxyInterface $proxy){
$answer = is_string($target->$prop) ? strtolower($target->$prop) : $target->$prop;
return $answer; //Only variables are returned by reference
});
$obj=(object)[
'test'=>'HELLO',
'other'=>'bay'
];
$proxy=new Proxy($obj,$handlers);
echo $proxy->test; // hello
echo $proxy->other;// BAY
use \Alpa\Tools\ProxyObject\Proxy;
use \Alpa\Tools\ProxyObject\ProxyInterface;
use \Alpa\Tools\ProxyObject\Handlers;
$handlers = new Handlers([
'get' => function & ($target, $name, ProxyInterface $proxy) {
$name = '_' . $name;
return $target->$name;
},
'set' => function ($target, $name, $value, ProxyInterface $proxy): void {
$name = '_' . $name;
$target->$name = $value;
},
'isset' => function ($target, $name, ProxyInterface $proxy): bool {
$name = '_' . $name;
return property_exists($target,$name) ;
},
'unset' => function ($target, $name, ProxyInterface $proxy): void {
$name = '_' . $name;
unset($target->$name);
},
'iterator' => function ($target, $proxy) {
return new class($target, $proxy) implements \Iterator {
private object $target;
private Proxy $proxy;
private array $keys = [];
private int $key = 0;
public function __construct(object $target, ProxyInterface $proxy)
{
$this->target = $target;
$this->proxy = $proxy;
$this->rewind();
}
public function current()
{
$prop = $this->key();
return $prop !== null ? $this->proxy->$prop : null;
}
public function key()
{
$prop = $this->keys[$this->key] ?? null;
return $prop !== null ? ltrim($prop, '_') : null;
}
public function next(): void
{
$this->key++;
}
public function rewind()
{
$this->key = 0;
$this->keys = array_keys(get_object_vars($this->target));
}
public function valid(): bool
{
$prop = $this->key();
return $prop !== null &&
isset($this->proxy->$prop);
}
};
}
]);
$target=(object)['_test'=>'test'];
$proxy=new Proxy($target,$handlers);
echo $proxy->test;// get $target->_test value return 'test'
$proxy->test='new value';// set $target->_test value
echo $proxy->test; // get $target->_test value return 'new value'
echo isset($proxy->test); // isset($target->_test) return true
foreach($proxy as $key=>$value){
echo $key; // test
echo $value; // $proxy->test value => $target->_test value
}
unset($proxy->test); // unset($target->_test)
echo key_exists($target,'_test'); // return false;
$handlers=new \Alpa\Tools\ProxyObject\Handlers\Closures([
// handler for members query
'get'=>function &($target,$prop,$proxy){},
// handler for members entry
'set'=>function($target,$prop,$value,$proxy):void{},
// handler for entry members
'unset'=>function($target,$prop,$proxy):void{},
// handler to check if members exist
'isset'=>function($target,$prop,$proxy):bool{},
// handler to call members
'call'=>function & ($target,$prop,$args,$proxy){},
// handler for invoke object or class
'invoke'=>function & ($target,array $args,$proxy){},
// handler for toString object or class
'toString'=>function($target,$proxy):string {},
// handler for delete members
'iterator'=>function($target,$prop,$proxy):\Traversable{},
]);
use Alpa\Tools\ProxyObject\Handlers\Instance;
class MyHandlers extends Instance
{
};
// to declare only static actions
use Alpa\Tools\ProxyObject\Handlers\StaticActions;
class MyHandlers extends StaticActions
{
};
// to declare only instance actions
use Alpa\Tools\ProxyObject\Handlers\InstanceActions;
class MyHandlers extends InstanceActions
{
};
use Alpa\Tools\ProxyObject\Proxy;
use Alpa\Tools\ProxyObject\ProxyInterface;
use Alpa\Tools\ProxyObject\Handlers;
class MyHandlers extends Handlers\Instance
{
/**
* member value query handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param Proxy $proxy - the proxy object from which the method is called
* @return mixed - it is necessary to return the result
*/
protected function & get ($target,string $prop,$value_or_args,ProxyInterface $proxy)
{
return parent::get($target,$prop,$value_or_args,$proxy);
}
/**
* member value entry handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param mixed $value_or_args - value to assign
* @param Proxy $proxy - the proxy object from which the method is called
* @return void
*/
protected function set ( $target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::set($target,$prop,$value_or_args,$proxy);
}
/**
* checking is set member handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return bool
*/
protected function isset ($target,string $prop,$value_or_args,ProxyInterface $proxy):bool
{
return parent::isset($target,$prop,$value_or_args,$proxy);
}
/**
* member delete handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args -irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return void
*/
protected function unset ($target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::unset($target,$prop,$value_or_args,$proxy);
}
/**
* Member call handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param array $value_or_args - arguments to the called function.
* @param Proxy $proxy the proxy object from which the method is called
* @return mixed
*/
protected function & call ($target,string $prop,array $value_or_args,ProxyInterface $proxy)
{
return parent::call($target,$prop,$value_or_args,$proxy);
}
/**
* invoke object
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop -irrelevant
* @param array $value_or_args - arguments to the called function.
* @param Proxy $proxy the proxy object from which the method is called
* @return mixed
*/
protected function & invoke($target, $prop, array $value_or_args, ProxyInterface $proxy)
{
return parent::invoke($target,$prop,$value_or_args,$proxy);
}
/**
* converting to string object or class
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop -irrelevant
* @param null $value_or_args -irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return string
*/
protected function toString($target, $prop, $value_or_args, ProxyInterface $proxy):string
{
return parent::toString($target,$prop,$value_or_args,$proxy);
}
/**
* creates an iterator for foreach
* @param object|string $target - observable object or class.
* @param null $prop - irrelevant
* @param null $value_or_args -irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return \Traversable
*/
protected function iterator ($target,$prop,$value_or_args,ProxyInterface $proxy):\Traversable
{
return parent::iterator($target,$prop,$value_or_args,$proxy);
}
/**
* member value query handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param Proxy $proxy - the proxy object from which the method is called
* @return mixed - it is necessary to return the result
*/
protected static function & static_get ($target,string $prop,$value_or_args,Proxy $proxy)
{
return parent::static_get($target,$prop,$value_or_args,$proxy);
}
/**
* member value entry handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param mixed $value_or_args - value to assign
* @param Proxy $proxy - the proxy object from which the method is called
* @return void
*/
protected static function static_set ($target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::static_set($target,$prop,$value_or_args,$proxy);
}
/**
* checking is set member handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return bool
*/
protected static function static_isset ($target,string $prop,$value_or_args,ProxyInterface $proxy):bool
{
return parent::static_isset($target,$prop,$value_or_args,$proxy);
}
/**
* member delete handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args -irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return void
*/
protected static function static_unset ($target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::static_unset($target,$prop,$value_or_args,$proxy);
}
/**
* Member call handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param array $value_or_args - arguments to the called function.
* @param Proxy $proxy the proxy object from which the method is called
* @return mixed
*/
protected static function static_call ($target,string $prop,array $value_or_args =[],ProxyInterface $proxy)
{
return parent::static_call($target,$prop,$value_or_args,$proxy);
}
/**
* invoke object
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop - object member name
* @param array $value_or_args - arguments to the called function.
* @param Proxy $proxy the proxy object from which the method is called
* @return mixed
*/
protected static function & static_invoke($target, $prop, array $value_or_args, ProxyInterface $proxy)
{
return parent::static_invoke($target,$prop,$value_or_args,$proxy);
}
/**
* converting to string object or class
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop -irrelevant
* @param null $value_or_args -irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return string
*/
protected static function static_toString($target, $prop, $value_or_args, ProxyInterface $proxy):string
{
return parent::static_toString($target,$prop,$value_or_args,$proxy);
}
/**
* creates an iterator for foreach
* @param object|string $target - observable object or class.
* @param null $prop - irrelevant
* @param null $value_or_args -irrelevant
* @param Proxy $proxy the proxy object from which the method is called
* @return \Traversable
*/
protected static function static_iterator ($target,$prop,$value_or_args,ProxyInterface $proxy):\Traversable
{
return parent::static_iterator($target,$prop,$value_or_args,$proxy);
}
};
use Alpa\Tools\ProxyObject\Proxy;
use Alpa\Tools\ProxyObject\ProxyInterface;
use Alpa\Tools\ProxyObject\Handlers;
class MyHandlers extends Handlers\InstanceActions
{
/**
* member value query handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param ProxyInterface $proxy - the proxy object from which the method is called
* @return mixed - it is necessary to return the result
*/
protected function & get ($target,string $prop,$value_or_args,ProxyInterface $proxy)
{
return parent::get($target,$prop,$value_or_args,$proxy);
}
/**
* member value entry handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param mixed $value_or_args - value to assign
* @param ProxyInterface $proxy - the proxy object from which the method is called
* @return void
*/
protected function set ( $target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::set($target,$prop,$value_or_args,$proxy);
}
/**
* checking is set member handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return bool
*/
protected function isset ($target,string $prop,$value_or_args,ProxyInterface $proxy):bool
{
return parent::isset($target,$prop,$value_or_args,$proxy);
}
/**
* member delete handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args -irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return void
*/
protected function unset ($target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::unset($target,$prop,$value_or_args,$proxy);
}
/**
* Member call handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param array $value_or_args - arguments to the called function.
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return mixed
*/
protected function & call ($target,string $prop,array $value_or_args,ProxyInterface $proxy)
{
return parent::call($target,$prop,$value_or_args,$proxy);
}
/**
* invoke object
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop - object member name
* @param array $value_or_args - arguments to the called function.
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return mixed
*/
protected function & invoke($target, $prop=null, array $value_or_args, ProxyInterface $proxy)
{
return parent::invoke($target,$prop,$value_or_args,$proxy);
}
/**
* converting to string object or class
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop -irrelevant
* @param null $value_or_args -irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return string
*/
protected function toString($target, $prop, $value_or_args, ProxyInterface $proxy):string
{
return parent::toString($target,$prop,$value_or_args,$proxy);
}
/**
* creates an iterator for foreach
* @param object|string $target - observable object or class.
* @param null $prop - irrelevant
* @param null $value_or_args -irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return \Traversable
*/
protected function iterator ($target,$prop,$value_or_args,ProxyInterface $proxy):\Traversable
{
return parent::iterator($target,$prop,$value_or_args,$proxy);
}
};
use Alpa\Tools\ProxyObject\Proxy;
use Alpa\Tools\ProxyObject\Handlers;
class MyHandlers extends Handlers\StaticActions
{
/**
* member value query handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param ProxyInterface $proxy - the proxy object from which the method is called
* @return mixed - it is necessary to return the result
*/
protected static function & get ($target,string $prop,$value_or_args, ProxyInterface $proxy)
{
return parent::get($target,$prop,$value_or_args,$proxy);
}
/**
* member value entry handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param mixed $value_or_args - value to assign
* @param ProxyInterface $proxy - the proxy object from which the method is called
* @return void
*/
protected static function set ( $target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::set($target,$prop,$value_or_args,$proxy);
}
/**
* checking is set member handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args - irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return bool
*/
protected static function isset ($target,string $prop,$value_or_args,ProxyInterface $proxy):bool
{
return parent::isset($target,$prop,$value_or_args,$proxy);
}
/**
* member delete handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param null $value_or_args -irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return void
*/
public static function unset ($target,string $prop,$value_or_args,ProxyInterface $proxy):void
{
parent::unset($target,$prop,$value_or_args,$proxy);
}
/**
* Member call handler
* @param object|string $target - observable object or class.
* @param string $prop - object member name
* @param array $value_or_args - arguments to the called function.
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return mixed
*/
protected static function & call ($target,string $prop,array $value_or_args,ProxyInterface $proxy)
{
return parent::call($target,$prop,$value_or_args,$proxy);
}
/**
* invoke object
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop - object member name
* @param array $value_or_args - arguments to the called function.
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return mixed
*/
protected static function & invoke($target, $prop, array $value_or_args, ProxyInterface $proxy)
{
return parent::static_invoke($target,$prop,$value_or_args,$proxy);
}
/**
* converting to string object or class
* by default the member in target must be a method
* @param object|string $target - observable object
* @param null $prop -irrelevant
* @param null $value_or_args -irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return string
*/
protected static function toString($target, $prop, $value_or_args, ProxyInterface $proxy):string
{
return parent::toString($target,$prop,$value_or_args,$proxy);
}
/**
* creates an iterator for foreach
* @param object|string $target - observable object or class.
* @param null $prop - irrelevant
* @param null $value_or_args -irrelevant
* @param ProxyInterface $proxy the proxy object from which the method is called
* @return \Traversable
*/
protected static function iterator ($target,$prop,$value_or_args,ProxyInterface $proxy):\Traversable
{
return parent::iterator($target,$prop,$value_or_args,$proxy);
}
};
use Alpa\Tools\ProxyObject\ProxyInterface;
use Alpa\Tools\ProxyObject\Handlers;
class MyHandlers extends Handlers\Instance {
protected static function & static_get($target,string $prop,$val_or_args,ProxyInterface $proxy)
{
$answer=is_string($target->$prop)?strtoupper($target->$prop):$target->$prop;
return $answer;
}
protected static function & static_get_test($target,string $prop,$val_or_args,ProxyInterface $proxy)
{
// $prop==='test';
$answer=is_string($target->$prop)?strtolower($target->$prop):$target->$prop;
return $answer;
}
};
$obj=(object)[
'test'=>'HELLO',
'other'=>'bay'
];
$proxy=new Proxy($obj,MyHandlers::class);
echo $proxy->test; // hello
echo $proxy->other;// BAY
use Alpa\ProxyObject\Handlers\Instance;
class MyClass{
public static $prop1='Hello';
public static $prop2='bay';
public static function method(int $arg)
{
return $arg+1;
}
}
class MyHandlers extends Instance{}
$proxy = new Proxy(MyClass::class,MyHandlers::class);
echo $proxy->prop1;// 'Hello';
$proxy->prop2='BAY';
echo MyClass::$prop2;// 'BAY';
echo isset($proxy->prop2);// true;
echo isset($proxy->no_prop);// false;
$proxy->prop2='test';// Errror:Cannot set new static class property
unset($proxy->prop2);// Errror:Cannot unset static class property
$proxy->prop2();// Errror:By default, you cannot call the property. But you can set a handler on the call action and the properties will be called according to the handler logic.
echo $proxy->method(1);// return 2
foreach($proxy as $key=>$value){
echo $key." && ".$value;
// prop1 && Hello;
// prop2 && BAY;
}
use Alpa\Tools\ProxyObject\Handlers\ActionsInterface;
use Alpa\Tools\ProxyObject\Proxy;
use Alpa\Tools\ProxyObject\ProxyInterface;
class MyHandlersClass implements ActionsInterface
{
public function & run(string $action, $target,?string $prop,$value_or_arguments,ProxyInterface $proxy)
{
}
public static function & static_run(string $action, $target,?string $prop,$value_or_arguments,ProxyInterface $proxy)
{
}
}
$target=(object)[];
$proxy = new Proxy ($target,MyHandlersClass::class);
$handlers=new MyHandlersClass ();
$proxy = new Proxy ($target,$handlers);
use \Alpa\Tools\ProxyObject\Handlers;
final class OtherProxy extends \Alpa\Tools\ProxyObject\ProxyAbstract {
public function __construct($target,$handlers)
{
//Warning: the types must match the property types in the description.
$this->target = $target;
$this->handlers = $handlers;
}
}
final class MyHandlersClass extends Instance {
}
$target=(object)[];
$proxy = new OtherProxy($target,MyHandlersClass::class);
var_dump($proxy instanceof OtherProxy);
//or
$handlers=new MyHandlersClass();
$proxy = new OtherProxy($target,$handlers);
var_dump($proxy instanceof OtherProxy);
// or
final class MyHandlersClass2 extends Instance {
}
use Alpa\Tools\ProxyObject\Proxy;
use Alpa\Tools\ProxyObject\ProxyInterface;
use Alpa\Tools\ProxyObject\Hanclers\Instance;
class MyHandlers extends Instance
{
protected bool $is_methods=false;
public function __construct(bool $is_methods=false){
$this->is_methods=$is_methods;
}
protected function isset ( $target,string $prop,$val,ProxyInterface $proxy):bool
{
if($this->is_methods){
return method_exists($target,$prop);
}
return property_exists($target,$prop);
}
}
class TargetClass
{
public $property='hello';
public function method(){}
}
$inst=new TargetClass();
$proxyProps=new Proxy($inst,new MyHandlers());
$proxyMethods=new Proxy($inst,new MyHandlers(true));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.