You might have found yourself at this php variable functions page because, like me, you wanted to pass functions
around like objects to client objects as you can in JavaScript. The issue I ran into was although
I could call a function using a variable like this " $v(); "...I could not do it like this " $obj->p() " where
'p' is a property containing the name of the method to call. Did not want to save my property off to a variable prior
to making my call: " $v = $obj->p; $v(); "; even if one finds a way, the below applies...
I credit this expanded work to this person: tatarynowicz at gmail dot com;
without them I would not have gotten here.
*/interfaceiface_dynamic_members{//Use of this interface enables type-hinting for objects that implement it.public function__call($name,$args);
public function__set($name,$value);
public functionquietly_fail():bool;
}
traittrait_has_dynamic_members{//Implementing these magic methods in the form of a trait, frees the client object up
//so it can still inherit from a parent-class.public function__call($name,$args) {
if (is_callable($this->$name)) {
returncall_user_func($this->$name,$args);
}
else {//Your dynamic-membered object can declare itself as willing to ignore non-existent method calls or not.if($this->quietly_fail()===true){
echo'Method does not exist, but I do not mind.';
}else{
echo'Method does not exist, I consider this a bug.';
}
}
}
public function__set($name,$value) {$this->$name=is_callable($value) ?$value->bindTo($this,$this):$value;//Assignment using ternary operator.}
}
abstract classMBR_ATTR{//A class full of attributes that objects can take on; abstract since not to be instantiated (If I could make it "final" as well, I would).public static functionis_a_walker(iface_dynamic_members $obj, ?string $walker_type='normal pace'){$obj->walker_type=$walker_type;$obj->walker_walk= function() {
return"I am walking{$this->walker_type}.";
};
}
public static functionis_a_runner(iface_dynamic_members $obj,string $runner_type){$obj->runner_type=$runner_type;$obj->runner_run= function() {
return"I am running{$this->runner_type}.";
};self::is_a_walker($obj);//If can run, also can walk.}
}
classcls_partly_dynamicimplementsiface_dynamic_members{
usetrait_has_dynamic_members;
public functionquietly_fail():bool{
returntrue;
}
}// Report all errors except E_NOTICEerror_reporting(E_ALL& ~E_NOTICE);//Enable all error-reporting except notices.
//----
//config runner object...$obj_runner= newcls_partly_dynamic();MBR_ATTR::is_a_runner($obj_runner,'fast');$obj_runner->runner_type='a bit slow';//----
//config walker object...$obj_walker= newcls_partly_dynamic();MBR_ATTR::is_a_walker($obj_walker,'slow');$obj_walker->walker_type='super fast';//----
//Do stuff...echo'walker in action...'.'
';
echo$obj_walker->walker_walk() .'
';
echo'
';
echo'runner in action...'.'
';
echo$obj_runner->walker_walk() .'
';
echo$obj_runner->runner_run() .'
';
echo$obj_runner->xxx() .'
';//Try calling a non-existent method.
//I would agree that the above approach/technique is not always ideal, particulary due to the loss of code-completion in your
//IDE of choice; I would tend to use this approach for dynamic-programming in response to the user dictating processing steps via a UI.?>