How to get app container in worker.php (Temporal PHP SDK)?

Hi all,
I try to register activity with extra dependency in worker.php. PHPDoc of WorkerInterface::registerActivity() method says that I should provide a callback to inject a dependency:

$worker->registerActivity(MyActivity::class, fn(ReflectionClass $class) => $container->create($class->getClass()));

But I’m confusing with $container variable: should I get instance of my Symfony Container here (I use Temporal PHP SDK as Composer dependency in my Symfony application) or should I implement my own container class?

Thanks for help in advance!

Hi @Inessa_M
I asked our PHP experts to take a look and provide info.

From what I can tell (could be wrong) you don’t need that $container var but can just pass in a function that returns your activity impl, see here. I also think you can pass in depends to your activity constructor when you create your activity instance.

Hi! It is just an example with a container. It can may anything. For example:

$worker->registerActivity(MyActivity::class, fn() => new MyActivity());

or inside your application that already has access to container and autowiring:

$worker->registerActivity(MyActivity::class, fn() => $this->youActivityInstance);

1 Like

Hi @tihomir , @Sergey_Zhuk, thanks for help! I add returning of my activity implementation into callback function and it works well.