Search code examples
phpsymfonyoopphp-8.1symfony6

I can not create a class instance from a string in Symfony


What I wanted to is to create class instance from string in Symfony framework to use Doctrine Repository. Here is my code block:

        $type = "SomeClassName";
        $productType = $this->productTypeProvider->getProductType($product->getBarcode());
    $repository = $this->doctrine->getRepository($type::class);

I want to use SomeClassName::class above but it gives me the following error. Any help would be appeciated:

Cannot use "::class" on value of type string (500 Internal Server Error)

Solution

  • As mentioned above, using the full class name worked perfectly.