Recently, I noticed one thing that began to bother me: PHP programmers do not use functions.
Perhaps, a too strong generalization, so I’ll clarify: PHP developers who have reached a certain level of skill almost stop writing normal functions, instead of them are solid classes and methods. In any case, this impression develops when you look at the code of libraries and open source frameworks. All the “cool” libraries use only anonymous functions, and only.
With other languages, the situation is not at all like that. Say, Python code will for the most part consist of class definitions, but there will also be room for ordinary functions.
In PHP, functions are usually used in two cases: the programmer does not know how to use the OOP, or the script is so simple that there is no point in defining the OOP. It seems there is no such thing as a helper function or something like that.
')
And in a sense, this worries me, I consider it wrong to shove it all into classes. This approach leads to Class-Oriented Programming, when the use of classes becomes an end in itself. (Let me remind you: the presence of classes in itself does not make the code object-oriented!)
In addition, I believe that the reason for the emergence of new and new functions in the core of the language lies precisely in the dislike for helpers. Just recently, several people asked to add
array_rand_value
. It would do the same as
array_rand
, only returning not the key, but the value. In the client code you can write it in two lines.
function array_rand_value(array $array) { if (empty($array)) return null; return $array[array_rand($array)]; }
Tiny feature, nothing. But here arises the most important difficulty in my opinion: where to place it? And how to make her friends with autoload?
PHP largely adopted the OOP model from Java, in particular, a one-to-one correspondence: one class - one file. The language itself does not require this, but it is already a generally accepted agreement. Moreover, it is supported by the autoload mechanism and the PSR-0 standard.
There are no other similar languages in PHP. For example, in Python it is quite normal to group several classes and functions in one file, since in this language the file is more like a module, a set of interrelated components. Obviously, with such an organization, it is much easier to find a place for functions.
I generally think that such a one-to-one correspondence class is a file that is a source of problems for PHP. In addition to the complexity of the functions, there are additional costs for creating small classes. Competent OOP often generates a large number of small classes, which is good in terms of support, code reuse and testing. But in PHP for each classic you need to get your own file. And it really infuriates. I am not at all embarrassed by a dozen short classes living in one file. But creating ten files for each of them really seems to me ineffective (and complicating support).
At the same time I will mention another “generally accepted” approach, which I do not like, these are redundant blocks of comments. In the lion's share of cases, phpdoc is set by Captain Obvious himself, the code from them swells two to three times. I have nothing against doblokov where they
are really needed , but in most cases (at least in good code from an architectural point of view) the name of the method and parameters speak for themselves.
So I think it might be worth being more concise: instead of creating a file for each class, merge small classes into one file. Instead of your class for every little thing, just write a function. Instead of clogging the code with useless dockblocks, leave them only when necessary.
Such are the thoughts. Maybe I do not understand everything at all?