Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
How to add Definiton of Symfony Dependency injection container builder from Class factory method?

For example: I have one class which have ClassA::create();

How to register this factory in ContainerBuilder?

$containerBuilder = new ContainerBuilder();

$containerBuilder
    ->register('access_manager', AccessManager::class)
    ->addArgument([new Reference('post_voter')]);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You call setFactory().

E.g.:

$containerBuilder = new ContainerBuilder();

$containerBuilder
    ->register('access_manager', AccessManager::class)
    ->addArgument([new Reference('post_voter')]);

$containerBuilder->register(ClassA::class)
                  ->setClass(ClassA::class)
                  ->setFactory([ClassA::class, 'create']);

If you want the factory to create new instances of the service each time it's accessed, you'd need to add a call to setShared(false).

Here you can see an example on an online sandbox: https://phpsandbox.io/e/x/qhhxv

(Click on "run" on the bottom right to see it working).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!