douzhang1852 2011-01-03 11:54
浏览 53
已采纳

在PHP中执行委托或回调的正确方法

I need to implement the following pattern in php:

class EventSubscriber
{
    private $userCode;
    public function __construct(&$userCode) { $this->userCode = &$userCode; }
    public function Subscribe($eventHandler) { $userCode[] = $eventHandler; }
}

class Event
{
    private $subscriber;
    private $userCode = array();

    public function __construct()
    {
        $this->subscriber = new Subscriber($this->userCode)
    }

    public function Subscriber() { return $this->subscriber; }

    public function Fire()
    {
        foreach ($this->userCode as $eventHandler)
        {
            /* Here i need to execute $eventHandler  */
        }
    }
}

class Button
{
    private $eventClick;

    public function __construct() { $this->eventClick = new Event(); }

    public function EventClick() { return $this->eventClick->Subscriber(); }

    public function Render()
    {
        if (/* Button was clicked */) $this->eventClick->Fire();

        return '<input type="button" />';
    }
}

class Page
{
    private $button;

    // THIS IS PRIVATE CLASS MEMBER !!!
    private function ButtonClickedHandler($sender, $eventArgs)
    {
        echo "button was clicked";
    }

    public function __construct()
    {
        $this->button = new Button();
        $this->button->EventClick()->Subscribe(array($this, 'ButtonClickedHandler'));
    }

    ...

}    

what is the correct way to do so.

P.S.

I was using call_user_func for that purpose and believe it or not it was able to call private class members, but after few weeks of development i've found that it stopped working. Was it a bug in my code or was it some something else that made me think that 'call_user_func' is able call private class functions, I don't know, but now I'm looking for a simple, fast and elegant method of safely calling one's private class member from other class. I'm looking to closures right now, but have problems with '$this' inside closure...

  • 写回答

3条回答 默认 最新

  • duanfazhun0383 2011-01-03 22:03
    关注

    Anyway, if someone's interested, I've found the only possible solution via ReflectionMethod. Using this method with Php 5.3.2 gives performance penalty and is 2.3 times slower than calling class member directly, and only 1.3 times slower than call_user_func method. So in my case it is absolutely acceptable. Here's the code if someone interested:

    class EventArgs {
    
    }
    
    class EventEraser {
    
        private $eventIndex;
        private $eventErased;
        private $eventHandlers;
    
        public function __construct($eventIndex, array &$eventHandlers) {
            $this->eventIndex = $eventIndex;
            $this->eventHandlers = &$eventHandlers;
        }
    
        public function RemoveEventHandler() {
            if (!$this->eventErased) {
                unset($this->eventHandlers[$this->eventIndex]);
    
                $this->eventErased = true;
            }
        }
    
    }
    
    class EventSubscriber {
    
        private $eventIndex;
        private $eventHandlers;
    
        public function __construct(array &$eventHandlers) {
            $this->eventIndex = 0;
            $this->eventHandlers = &$eventHandlers;
        }
    
        public function AddEventHandler(EventHandler $eventHandler) {
            $this->eventHandlers[$this->eventIndex++] = $eventHandler;
        }
    
        public function AddRemovableEventHandler(EventHandler $eventHandler) {
            $this->eventHandlers[$this->eventIndex] = $eventHandler;
    
            $result = new EventEraser($this->eventIndex++, $this->eventHandlers);
    
            return $result;
        }
    
    }
    
    class EventHandler {
    
        private $owner;
        private $method;
    
        public function __construct($owner, $methodName) {
            $this->owner = $owner;
            $this->method = new \ReflectionMethod($owner, $methodName);
            $this->method->setAccessible(true);
        }
    
        public function Invoke($sender, $eventArgs) {
            $this->method->invoke($this->owner, $sender, $eventArgs);
        }
    
    }
    
    class Event {
    
        private $unlocked = true;
        private $eventReceiver;
        private $eventHandlers;
        private $recursionAllowed = true;
    
        public function __construct() {
            $this->eventHandlers = array();
        }
    
        public function GetUnlocked() {
            return $this->unlocked;
        }
    
        public function SetUnlocked($value) {
            $this->unlocked = $value;
        }
    
        public function FireEventHandlers($sender, $eventArgs) {
            if ($this->unlocked) {
                //защита от рекурсии
                if ($this->recursionAllowed) {
                    $this->recursionAllowed = false;
    
                    foreach ($this->eventHandlers as $eventHandler) {
                        $eventHandler->Invoke($sender, $eventArgs);
                    }
    
                    $this->recursionAllowed = true;
                }
            }
        }
    
        public function Subscriber() {
            if ($this->eventReceiver == null) {
                $this->eventReceiver = new EventSubscriber($this->eventHandlers);
            }
    
            return $this->eventReceiver;
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line