Always use a FQCN as an identifier.
What do I mean by this? Well, here is few examples.
All too often we see things like this:
Do NOT do this.Why? If for no other reason it tells static analysis and your IDE zero. 'Some\NameSpace\DependencyClass' by itself is just a string. its a code smell. Bad practice. Blah blah. You get the same results with A LOT more benefits with the following:
Take away:
Write code that it strongly typed, easy to read, and maintainable because it is testable from the start and you will have fewer issues throughout your development cycles.
What do I mean by this? Well, here is few examples.
All too often we see things like this:
PHP:
$instance = $container->get('Some\NameSpace\DependencyClass');
Do NOT do this.Why? If for no other reason it tells static analysis and your IDE zero. 'Some\NameSpace\DependencyClass' by itself is just a string. its a code smell. Bad practice. Blah blah. You get the same results with A LOT more benefits with the following:
PHP:
<?php
// sudo code
declare(strict_types=1);
use Psr\Container\ContainerInterface;
use Some\NameSpace\DependencyClass;
use Some\NameSpace\ServiceClass;
/**
* Usually in a factory
* @param ContainerInterface $container
* @return ServiceClass
*/
public function __invoke(ContainerInterface $container): ServiceClass
{
if(! $container->has(DependencyClass::class)) {
// throw exception
}
/** @var DependencyClass */
$dep = $container->get(DependencyClass::class);
return new ServiceClass($dep);
}
PHP:
<?php
declare(strict_types=1);
use Some\Target\ClassName;
/** @var array<class-string, array> */
$config = [
ClassName::class => [
'some_config_key' => 'some_config_value',
],
];
Take away:
Write code that it strongly typed, easy to read, and maintainable because it is testable from the start and you will have fewer issues throughout your development cycles.
Last edited: