Lilian Masnada
Lilian Masnada

Reputation: 107

symfony can't load a class with classname set dynamically

I create a service in symfony 3.2

I want to call a different class set by a parameter

public function setPaymentMethod($paymentMethod){
    $this->datas = $paymentMethod->getDatas();
    $className = ucfirst($this->datas["MODULE_NAME"]);
    new $className($this->datas);
}

In this case the code try to load the class Spplus which is defined in my service with a use statement I get this error:

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

If I try to load "manually" Spplus class it works

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

Upvotes: 0

Views: 1085

Answers (1)

Lilian Masnada
Lilian Masnada

Reputation: 107

As said by Cerad It works with a fully qualified classname.

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);
}

Upvotes: 1

Related Questions