douminfu8033 2015-01-02 19:09
浏览 8
已采纳

访问传递给child方法的参数

I have the following:

class bar {

    function __construct(){

         // set $a_from_fire to $a from fire() method arg
         // set $b_from_fire to $b from fire() method arg
    }
}

class foo extends bar {

    function fire ($a, $b){

    }
}

I need to set $a_from_fire and $b_from_fire using the arguments from foo->fire()

So if I do this:

$test = new foo;
$test->fire(1, 2);

These vars will be set:

$a_from_fire == 1; // true
$b_from_fire == 2; // true
  • 写回答

2条回答 默认 最新

  • douba8048 2015-01-02 19:53
    关注

    I don't think you can do it in any "correct" way. My first thought was to use __call, but that of course is only called for undefined functions.

    And there's not really any way to dynamically rename the methods, unless you're already using RunKit. (not that I know of or could find anyway).

    If it's purely for debug purposes, you could set your own class autoloader to pre-process the files, change the method names, and then use the __call magic method on your parent class.

    spl_autoload_register(function($class){
           $hackPath = '/home/_classes/'.$class;
           if (!file_exists($hackPath)){
               $realPath = '/home/classes/'.$class;
               $file = file_get_contents($realPath);
               $processedContent = //use regex or something to prepend all function names with an _.
               file_put_contents($hackPath,$processedContent);
           }
    
    
           require_once $hackPath;
        });
    

    Then in your parent class

    class parent {
    
        public function __call($funcName,$arguments){
    
           $this->myLogFunc($funcName,$arguments);
           //since you prepended with an underscore
           return call_user_func_array('_'.$funcName,$arguments);
    
        }
    

    This is a terrible way to do what you're asking, but it could work. The pre-processing of files might be slow, but you'd only need to do it if the originals changed (you can use filemtime to check if it's changed).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制