clean-code-php icon indicating copy to clipboard operation
clean-code-php copied to clipboard

Don't write to global functions (classes?)

Open andreyvolosyuk opened this issue 6 years ago • 1 comments

https://github.com/jupeter/clean-code-php#dont-write-to-global-functions

Could you explain please why you suggest class as a alternative of function in this case? Why classes whouldn't conflict between each other like functions in that example? And why namespaces and naming convenction wouldn't resolve this sort of issue?

I guess such kind of a solution would work fine as well:

use function \App\Helpers\config as myConfig; //just for instance
use function \Path\To\Imported\Package\config;

$app = new Application();

$app->setConfig(myConfig());
$app->setConfig(config());

andreyvolosyuk avatar May 14 '18 09:05 andreyvolosyuk

I think the problem is related to the fact that this section is copied and not very well adapted for PHP.

Of course, the introduction of namespaces is also a solution to the problem.

I would emphasize that Configuration class should be used as Dependency Injection, and config() function, even in namespace, is global and can be used anywhere in the program as a global variable.

peter-gribanov avatar May 21 '18 09:05 peter-gribanov