example函数php_PHP: 可变函数 - Manual

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.?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值