I write a small PHP script, which is desirable to make a single file. At the same time I try to comply with PSR standards, one of them says:

Code written for PHP 5.3 and newer versions MUST use formal namespaces.

In fact, I have only 2 classes, using space in this situation is not very convenient. If I break this standard in this situation, will it be regarded by experts as an error?

  • Why is expert opinion important to you? Are you developing a script that you will publish in open access? Do you plan to apply in the "portfolio"? - tutankhamun
  • 2
    In fact, I have only 2 classes, using space in this situation is not very convenient. - Speech about one line of code. Exactly uncomfortable? - etki
  • This is a cli-application, for some reason it should be written in php. On Linux, it should be run by a specific command, most likely using bash aliases. And yes, maybe it will fall into open access. - Filipp Mustang
  • in the cli script that you plan to use autonomously (without using it as a library) you can (and should) put on this requirement, otherwise you will start packing every second line in your namespace, and if you plan to use the script as a library, you wrote the script incorrectly and the library is wrong. - strangeqargo

3 answers 3

At the same time I try to comply with PSR standards.

Decide why you are doing this.

This, apparently, is about the PSR-1. And here you need to give a report that this standard was adopted in order to make the code easier to understand, reuse and identify the author. Ie, anyway, for collective work on the code.

If you are not going to share with anyone, then you can not declare a namespace.

But if you want to put it somewhere, or just to fill the habit, then follow the standards and get used to insert the namespace. One line namespace \Fmustang\ToyProject; It does not require effort (especially since you can configure IDE to do this automatically), but it seems to teach you to have a good tone.

    Yes of course. This is the deepest violation of generally accepted conditions. Following standards means 1) discipline, 2) organized code according to the rules that others follow, 3) Your code is suitable for use along with others on this standard, 4) Makes life easier when working in a team and. etc.

    Yes, maybe it works. Yes, it can be a cool code. But it is difficult to maintain it as it grows. Every time you have to climb into it to understand where everything lies. And most likely you will refuse it later on.

    Imagine a situation. You and 3-4 people are assembling cars. They are by standards (steering wheel in front, pedals under your feet, and so on). And you, because you took it into your head. Buyers come up and naturally begin to ask you - Where are the steering wheel, the pedals ...? You - Yes, over there and there. Guess whose machine they will use 3 times :) Do not be a crooked programmer whose slogan is one - There is nothing so pure and light that you can’t take it by hand.

    Initially, you will experience discomfort, this is natural. And remember that these standards are not invented by stupid people. Those who probably have more experience and knowledge.

    So my advice is to use the standards that smart people invented it more correctly. I hope to help. Good luck

    PHP Standards Recommendations

      Any standard is nothing more than a recommendation. It is up to you to break or follow it. Especially when it comes to personal projects. For example, in the Yii framework, there is one class without a namespace (in the global space). And nothing, everything lives, breathes and is popular.

      In this situation, you can do this: give the classes a namespace and create another file that will use these classes. And then pack the whole thing into an executable phar file .