我正在尝试构建一个简单的类,它将基于提供的字符串将类名存储在实例变量中,然后在该类名上调用静态方法,如下所示:
$obj = new MyObject();
$obj->setClass('myclass');
$obj->callSomeMethod();
现在,MyObject将实现类似的功能:
class MyObject {
public function setClass($class) {
switch($class) {
case: 'myclass':
// stores MyOtherClass in instance variable
$this->storeClass = MyOtherClass
}
}
public function callSomeMethod() {
// retrieve previously set class
$class = $this->storeClass;
$class::staticMethodCall();
}
}
现在这个工作,在传递的类上调用静态方法,但是我也得到PHP注意:
PHP Notice: Use of undefined constant MyOtherClass - assumed 'MyOtherClass'
我应该改变什么才能摆脱它?