Require_once at the beginning or when is it really necessary?

Where can I put instructions require_onceand why?

  • Always at the beginning of the file, before the class,
  • In the actual method, when the file is really needed
  • It depends on the

Most put frameworks are included at the beginning and don't care if the file is really needed.
Using autoloader is a different case.

Edit:

Of course, we all agree that autoloader is the way to go. But this is a "different case", I was not asking here. (BTW, the Zend Framework Application uses an autoloader, and the files still require strict requirements and are located at the beginning).

I just wanted to know why programmers include the required files at the beginning of the file, even if they probably will not be used at all (for example, exception files).

+3
source share
2 answers

I would say 3. It depends. If you are dealing with a lot of code, it may be worth downloading the included file only on request, since downloading the code will take time and eats up memory. On the other hand, this makes maintenance much more difficult, especially if you have dependencies. If you download on-demand, you can use a wrapper function so that you can track which module is loaded where.

, - - , .

+3

- , , . , , __autoload, - .

"includes.php", __autoload (, , ..). .

+5

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


All Articles