Hi Manuel,

pt., 26 lut 2021 o 09:17 Manuel Canga <p...@manuelcanga.dev> napisał(a):

> Hello, another example with "factories method"[1]:
>
> ```php
> use MyProject\Framework;
>
> abstract class AbstractController {
>     private const HELPER_PATH = static::namespace.'/Helpers';
>     private const SERVICE_PATH = static::namespace.'/Services';
>
>      public function instanceHelper( string $helperClass ) {
>             $helperClassName = self::HELPER_PATH."/{$helperClass}";
>
>            return new $helperClassName();
>     }
>
>    public function instanceService( string $serviceClass )  {
>             $serviceClassName = self::SERVICE_PATH."/{$serviceClass}";
>
>            return new $serviceClassName();
>    }
> }
>
> use MyProject\MyModule;
>
> class Controller {
>     public function __invoke() {
>                  //......
>        $date = $this->instanceHelper('Date');
>               //...
>     }
>
> }
> ```
>
> [1]: https://en.wikipedia.org/wiki/Factory_method_pattern


Personally, none of the above examples is convincing bc I'd implement them
using a fixed class names map to avoid loading untrusted classes.
Any kind of helper class is something I'd personally not use to avoid
static coupling of code and hacks while writing unit tests over
services that could be injected by IoC.
Full class names (whether they're aliased or used in use clause) have more
benefit over class name string operations as they're easy for any renaming
which most of the IDE's these days can handle.
IMO introducing namespace magic constant has relatively narrow use and I'd
probably vote NO on this.

Cheers,
Michał Marcin Brzuchalski

Reply via email to