How might I define an array of methods within a PHP class? -
i need define several methods within class. methods similar, create array of method names , generate of methods array of names. don't need call methods, define them they're callable elsewhere.
i don't have define these methods way, method names set in stone.
something maybe:
class projectcontroller { public function __construct() { $this->makemethods(); } public function makemethods() { $methods = ['documents', 'addenda', 'docreleases', 'drawings']; foreach($methods $m){ $method_name = 'get' . $m; /* * define method named $method_name on projectcontroller * (i know statement below wrong. how might fix it? i'm i'm using '$this' incorrectly here, i'm not sure use. '$this' should reference projectcontroller.) */ $this->$method_name = function(){ // } } } }
this __get()
magic method for. no need have getters of variable class members exist. fetch them dynamically.
public function __get($var) { return $this->$var; }
Comments
Post a Comment