symfony无法加载动态设置类名的类

时间:2017-09-23 22:45:49

标签: symfony class

我在symfony 3.2中创建了一个服务

我想通过参数

调用另一个类
public function setPaymentMethod($paymentMethod){
    $this->datas = $paymentMethod->getDatas();
    $className = ucfirst($this->datas["MODULE_NAME"]);
    new $className($this->datas);
}

在这种情况下,代码尝试使用use语句加载我的服务中定义的类Spplus 我收到这个错误:

Attempted to load class "Spplus" from the global namespace.
Did you forget a "use" statement?

如果我尝试加载"手动"它运行的Spplus类

public function setPaymentMethod($paymentMethod){
    $this->datas = $paymentMethod->getDatas();
    new Spplus($this->datas)
}

1 个答案:

答案 0 :(得分:1)

如Cerad所说,它使用完全限定的类名。

public function setPaymentMethod($paymentMethod,$order){
    $this->datas = $paymentMethod->getDatas();
    $className = "SiteBundle\\Service\\PaymentMethod\\" . ucfirst($this->datas["MODULE_NAME"]);
    $this->paymentMethod = new $className($this->datas,$order,$this->rootDir);
}