i have class look like this:
class Foo
{
puclic static function blindPaths($paths)
{
foreach($paths as $name=>$path)
{
$method='set'.ucfirst($name).'Path';
if(method_exists(????,$method))
self::$method($path);
}
}
public function setBasePath($base)
{
//do something...
}
public function setAppPath($app)
{
//do something...
}
....
}
now, i call:
$paths = array(
'base'=>'path.of.base.path',
'app'=>'path.of.app.path',
'someValue'=>'path.of.someValuePath',
....
);
Foo::blindPaths($paths);
problem when check method_exists, what to fill in those marks "????" somebody can help me?
if(method_exists(__CLASS__, $method))
In a simple, single class situation you could use the __CLASS__
constant as a first argument for the method_exists
call, but if you're in a situation where the static method is defined at the parent level (or an abstract class, or some place else), then perhaps you may want to consider this:
puclic static function blindPaths($paths)
{
$current = get_called_class();
foreach($paths as $name=>$path)
{
$method='set'.ucfirst($name).'Path';
if(method_exists($current,$method))
self::$method($path);
}
}
Or, if you add interfaces and Trait
's to the mix:
puclic static function blindPaths($paths)
{
$current = get_called_class();
$current = new $current;//create instance
foreach($paths as $name=>$path)
{
$method='set'.ucfirst($name).'Path';
if($current instanceof Foo)
self::$method($path);
elseif ($current instanceof Bar)
return $this->{$method}($path);
}
}
But either way, rethink your design. If you're using a construct similar to what you have now, 9 out of 10 times, you're barking up the wrong tree.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.