1

I've just started working on a larger preexisting code base (PHP) and it has come to my attention that a lot of these files use includes/requires in an unreadable way. The includes/requires sort of cascade through the file system, because almost every file in the system includes/requires a file called common.php. This common file includes/requires just about every other file, so it seems that when a developer was in doubt, they just included/required common and called it good.

This was written by professional web developers, and I'm still a student, so I'm curious if this is common practice in PHP. In PHP should there be a common file that includes/requires everything, should each file be included/required case by case, or is there a different way that is common practice?

On a side note: I'm trying to switch out an api file for a new one (while keeping the old for compatibility testing), and it is difficult for me to tell which files use my new api or the old one.

TheBat
  • 1,006
  • 7
  • 25
  • 1
    No it's not common practice to just put every include in one file. – Daan Oct 06 '14 at 14:07
  • This is more of a case by case situation, however you may want to evaluate the differences between include and require here: http://stackoverflow.com/questions/3633900/difference-between-include-and-require-in-php – Mr. Concolato Oct 06 '14 at 14:09
  • At the very least, it might be a good idea to use the _once version if it's that messy: http://php.net/manual/en/function.include-once.php (there's also require_once) – Mike Oct 06 '14 at 14:09
  • 1
    A common practice is indeed to use bad approach, like the one you described. Many self-proclaimed professional developers do it. What you described is a bad approach. The correct approach would be loading includes when needed, also called "lazy loading". This is why cool people from php.net gave us autoloaders. And more aware and modern (real professional) developers use it. They go even further and use dependency manager tool called Composer to manage dependencies and autoloading, making this whole ordeal with loading, including and what not a thing of the past. – N.B. Oct 06 '14 at 14:09
  • Yes its depressingly common, especially in older code bases, no its not good practice. Good practice would be to use modular OOP code and an autoloader: http://php.net/manual/en/function.spl-autoload-register.php. However legacy code is what it is. – Steve Oct 06 '14 at 14:24

2 Answers2

3

Generally, includes should be used for view based material (HTML, Smarty, perhaps XML or XSLT) as in mark up used to construct a view in an MVC approach. However, require(); or require_once(); are used to control the incorporation of server side core functionality in a functional or object oriented setting (Controllers and Models in MVC).

Both require(); or require_once(); provide a programmatic safety mechanism where if the code fails, the code stops running and errors are sent to the logs - See documentation for require and require_once. Where as include(); will continue to run erroneous code despite errors - you do not want that.

To your other question concerning one file that "includes everything", some PHP frameworks like Zend and CodeIgniter allow for something called an auto loader - this loads all the available classes and instantiates the main core classes for the programmer to use objects at will.

If you are not using a framework, you can somewhat simulate this by creating a config or in your case a common file. However, you may not want to just arbitrarily load everything, as it could create confusion when other developers are trying to troubleshoot later - as you might be finding out now. You will have to evaluate, test and re-architect your code to figure out what is really common and what is sparsely used throughout your code. I have done this in the past by tracing - adding error_log() and var_dump() in an effort to follow the data as it moves through the code.

Mr. Concolato
  • 2,224
  • 5
  • 24
  • 44
  • Sorry for the confusion, I was using require and include interchangeably. I understand the difference between the two, I'm more curious about the over all include structure of the system. I've edited my question. – TheBat Oct 06 '14 at 14:56
  • @TheBat I have made edits to address your issue a bit more surgically I hope ;) – Mr. Concolato Oct 06 '14 at 15:17
  • Thanks, that clears things up. I was just trying to figure out if I should propose to management a refactor or not, but I guess I'll just deal with it haha. Thanks for the tips about debugging the include cascade, I'll just have to figure out the code for what it is. – TheBat Oct 06 '14 at 15:54
0

While you're getting the hang of PHP, it might be a good time to get familiar with PSR. You can find the definitions of the various PSR standards over at the PHP-FIG website. You will also find dozens of great tutorials on the topic (I'll let google handle the links for that).

Specific to your question are PSR-0 and PSR-4, each dealing with autoloading. Debates on autoloading, and PSR, continue and not everyone has a favorable opinion. Whether one likes it or not, it is a reality that will be encountered more frequently, and be able to understand and take advantage of it will greatly help your comfort level as you go forward.

Following PSR does make your code easier to read for people outside of your organization, and can make it easier to publicly share your project through composer/packagist.

Nathan
  • 83
  • 1
  • 6