Suppose if you wanted to call few methods from another object. What is the proper way of doing it.
And if you use __call()
, is it possible to extract the arguments, instead of using it as array.
Example:
<?php
class Component
{
protected $borrowMethods = array();
public function __call( $name, $args )
{
if( isset( $this->borrowMethods[$name] ) )
{
$obj = $this->borrowMethods[$name] ;
return $obj->$name( $this->argExtractFunc($args) );
}
throw new \Exception( 'method not exists' );
}
}
class ActiveRecord extends Component
{
protected $validator; //instance of validator
protected $borrowMethods = array(
'validate' => 'validator',
'getError' => 'validator',
'moreMethods' => 'someOtherClass',
);
public function save()
{
if($this->validate())
{
}
}
}
class Validator
{
public function validate(){}
public function getError( $field ){}
}
$ar = new ActiveRecord;
$ar->getError( $field );