I need to define several methods within a class. The methods are very similar, so I would like to create an array of the method names and then generate all of the methods from this array of names. I don't need to call the methods, just define them so that they're callable elsewhere.
I don't necessarily like that I have to define these methods this way, but the method names are set in stone.
Something like this maybe:
class ProjectController
{
public function __construct()
{
$this->makeMethods();
}
public function makeMethods()
{
$methods = ['Documents', 'Addenda', 'Docreleases', 'Drawings'];
foreach($methods as $m){
$method_name = 'get' . $m;
/*
* Define a method named $method_name on ProjectController
* (I know the statement below is wrong. How might I fix it? I'm almost certain that I'm using '$this' incorrectly here, but I'm not sure what to use. '$this' should be a reference to ProjectController.)
*/
$this->$method_name = function(){
// do something
}
}
}
}