duanjing4667 2015-01-03 22:48
浏览 114
已采纳

在回调中使用$ this指的是回调类

So, I have something like this:

class ClassA{

    public function doCallback($callback){
        call_user_func_array($callback, array());
    }

    public function doSomething(){
        return 12345;
    }

}


class ClassB{

    public function runMe(){
        $classA = new ClassA();

        $classA->doCallback(function(){
            $this->doSomething();
        });

    }

}

I am trying to figure out how, if possible I can use $this or something similar in a callback function that will refer to the class that the callback is running on (Not the class it is in) if that makes sense.

So in my above example I would like $this->doSomething(); where $this means ClassA and not ClassB. Currently $this is referring to ClassB. Is there something that I can use to say I want ClassA?

EDIT

Here is the actual method that I am using

public function save(){
    $id    = (int)$this->input->post("id");
    $title = $this->input->post("title");
    $uid   = (int)$this->session->get("id");

    $this->db->getTable("content")
    ->has(array("user_id" => $uid, "name" => $title), function(){
        echo json_encode(array("error" => "Name already exists."));
    }, function() use($id, $uid, $title){
        //$this->db->getTable("content")
        $this->update(array("name" => $title), array(
            "user_id"    => $uid,
            "content_id" => $id), function($rows){
            if($rows > 0){
                $error = "";
            }else{
                $error = "Unknown error occurred";
            }
            echo json_encode(array("error" => $error));
        });
    });
}

$this->db->getTable("content") returns a Database Object, and has() is a method in the object. I was hoping that I could use a shorthand way to access $this->db->getTable("content") without having to call it again in the callback, or passing it through call_user_func_array as a parameter or without the use of use().

the method has():
https://github.com/ZingPHP/Zing/blob/master/Zing/src/Modules/Database/DBOTable.php#L311

EDIT

I think in my callback function I need to do something like this, but I don't think it is working:

public function myFunc(callback $callback){
    $callback->bindTo($this, $this);
    return call_user_func_array($callback, array());
}
  • 写回答

2条回答 默认 最新

  • doujia2386 2015-01-03 23:49
    关注

    I got it! I just need to add $newCallback = $callback->bindTo($this, $this); in the callback class, and then in ClassB I can use $this to refer to ClassA.

    class ClassA{
    
        public function doCallback($callback){
            $callback = $callback->bindTo($this, $this);
            call_user_func_array($callback, array());
        }
    
        public function doSomething(){
            return 12345;
        }
    
    }
    
    
    class ClassB{
    
        public function runMe(){
            $classA = new ClassA();
    
            $classA->doCallback(function(){
                $this->doSomething();
            });
    
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch