note 95124 deleted from functions.anonymous by cmb

From: Date: Sun, 08 Dec 2019 11:31:10 +0000
Subject: note 95124 deleted from functions.anonymous by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213467@lists.php.net to get a copy of this message
Note Submitter: puskulcu at gmail dot com ---- hello there! here is a little code which shows use of the closures as event handlers: <?php class Button { public $OnBeforeClick; public $OnAfterClick; public $Name; function Button() { $this->Name = 'MyButton'; } public function Click() { $this->DoBeforeClick(); echo 'Click!'; $this->DoAfterClick(); } private function DoBeforeClick() { if (isset($this->OnBeforeClick)) { $Event = $this->OnBeforeClick; $Event($this); } } private function DoAfterClick() { if (isset($this->OnAfterClick)) { $Event = $this->OnAfterClick; $Event($this); } } } //eclipse may warn here about syntax error but no problem, it runs well. $BeforeClickEventHandler = function($Sender) { echo $Sender->Name . ' (Before Click)'; }; $AfterClickEventHandler = function($Sender) { echo $Sender->Name . ' (After Click)'; }; $MyWidget = new Button(); $MyWidget->OnBeforeClick = $BeforeClickEventHandler; $MyWidget->OnAfterClick = $AfterClickEventHandler; $MyWidget->Click(); ?> output: MyButton (Before Click) Click! MyButton (After Click) i hope you find this useful. regards. emre

« previous php.notes (#213467) next »