php - Trouble setting up closures for class methods -
i architecting out class , far have following
in main.php
$coolobject = new coolobject(true); foreach($array $key=>$val) { $coolobject->dofunction($val); }
then within of coolobject:
class coolobject { private $_usefunction; public function __construct($usefoo) { //this need help if($usefoo){ $this->_usefunction = $this->_foo(); } else{ $this->_usefunction = $this->_bar(); } } public function dofunction($values) { $this->_usefunction($values); } private function foo($values){ //do stuff } private function bar($values){ //do other stuff } }
i realize set if statement or switch dofunction handle functionality, since i'm not hip on closures/lamdas wasn't sure if right situation them or if i'm totally off base
what you're trying accomplish (as far know) function pointers in php.
you should take in: array pointer function in php
i still think inheritance easiest/cleaniest way solve this.
so end with:
class coolobject { private $_usefunction;
public function __construct($usefoo) { //this need help if($usefoo){ $this->_usefunction = 'foo'; } else{ $this->_usefunction = 'bar'; } } public function dofunction($values) { //edited @newacct corrects $this->$_usefunction($values); } private function foo($values){ //do stuff } private function bar($values){ //do other stuff }
}
php closures lambda
No comments:
Post a Comment