Should we use name functions in PHP?

We all know that namespacing your application code is PSR best practice and recommendation. The only things PSR can do are gaps and side effects when it comes to functions and their declaration.

But what about functions that are not associated with any class? Should they be in the namespace or remain in the global namespace?

Some developers say that namespace functions are bad practice, but I couldn't understand why.

From what I learned / read and saw recommended regarding namespacing functions:

  • NO: do not use global functions without classes, as they cannot be automatically loaded (this means creating supporting static classes or the like).
  • NO: do not use global functions, since they cannot be imported / used (the full name of the function is always required).
  • YES: Use namespaces for global functions to prevent collisions and to indicate functions that are not part of PHP itself (you can safely assume that you are calling the correct function).
  • NO: do not use namespaces for global functions, because functions are not bound to the OO anyways application structure (has a point, but see the previous point).

? , , , , (, , ..)?

+4
3

- , . . , . , , , , , .

, , . OO, Java #, . , . ( ).

, , , , SO. .

+3

[...] , , (, , ..)?

. - . , .

- . , , , , ike_rsqrt. , , , .

- , , , . , , , , , . , . . , , , - - . , , - , : .

- . , . , , , .

+1

, ?

PHP-. . , - , . , \Util\Str::camelize() UI\Util\TextCleaner::prettifyHtml().

?

Namespacing . : , . SRP.

, , . , , , .

: , ( ).

, . include() . ;)

: , / ( ).

, , . . / , .

: , PHP ( , ).

, . SOLID . . SOLID, ... , .

: , OO ( , . ).

, , .

?

. , - " , " . , . - , - .

, , (, , ..)?

, . - , . namespacing , , , .

+1
source

Source: https://habr.com/ru/post/1616020/


All Articles