Method Calls with Arbitrary Names ################################# :category: PHP :tags: :date: 2020-07-24 07:20:00 +0300 A discovery of the day. Many people know that you can define and read an object property with an arbitrary name: .. code-block:: php {'any string here'} = 123; echo $s->{'any string here'}; // 123 .. TEASER_END This is useful from time to time when parsing json for example: .. code-block:: php {'any string here'}; It's a more rare knowledge that you can also call methods with this syntax. Probably because it's not usually useful, after all you cannot define a method with a name from an arbitrary string. However you can catch such call with a magic method: .. code-block:: php {"any string here"}(123); // 'any string here', [123] // also works with other indirect calls call_user_func([$cc, "any string here"], 123); $func = "any string here"; $cc->$func(); It works in all versions of PHP after 5.0: https://3v4l.org/YCCE3, also with static context and ``__callStatic`` since 5.3. I don't know if this trick has many uses but for example it looks good as a `Symfony EventSubscriber implementation `_. .. _es: https://symfony.com/doc/current/components/event_dispatcher.html#using-event-subscribers .. code-block:: php SomeListener::class, ]; } public function __construct(ContainerInterface $container) { $this->container = $container; } public function __call(string $name, array $arguments): void { $listener = $this->container->get($name); $listener->onEvent($arguments[0]); } }