Search code examples
phpopen-sourceincludeconstants

Strategy for including and organizing files in PHP scripts


I'm new to Stack Overflow and PHP programming in general and have a question about including files in my scripts using PHP's require() and require_once() functions.

I am in the process of building an open source Web app in PHP and am wrestling with how to work with included files. I want to separate the core functions of the program from the actual code for each page and the HTML of each page, and currently have several include files, one for database-related functions and variables, one for general site/program settings, and another for other functions. Currently, all of these files are referenced by a single file called bootstrap.php, whose contents can be found at https://code.google.com/p/audioshout/source/browse/inc/bootstrap.php.

However, I've been told by a few people that this strategy is not a good one but none of them actually gave me a better strategy to use. As a result of my current strategy, warnings related to a particular function are generated on every page of the site even though that particular function is only needed on a few pages. But because it is included in a file that is the included by bootstrap.php, which in turn is included on every page of the site, the function gets called on every page visit and thus a warning is thrown.

What's the best way for me to clean up my inclusion strategy, or what would the best strategy be for including files in a PHP project? Also, how do constants come into play when working with includes and when creating settings for a project? Right now all settings, even those that are user-configurable, are stored in variables rather than constants, but I have seen other projects like WordPress use constants rather than variables, especially when it comes to paths for included files and etc.

Thanks for any answers provided! To see more source code for this project, please visit http://code.google.com/p/audioshout/.


Solution

  • Generally there's really never a use for using require over require_once. The latter protects you from errors or warnings about redefining functions and such. There might be an exception to this but it's never come up in my personal experience, which has been ten years or so. (Anyone else remember when PHP used to stand for 'personal home page'? :) )

    As for constants vs. variables, PHP is a very loose (weak typed) language. You can compare a string to a number, something that would never work in a strong typed language. You can also see that by just noticing how the function names are sometimes with an underscore, sometimes without, sometimes the first parameter of a string function will be the input, other times it will be the last.

    Point is, it's a great powerful tool but it was never intended to be what it came to be so it can be confusing at times to figure out the 'right' way to do things. It is layers upon layers of contributions from so many people. So you can try to do things as perfectly as you can but in those situations, really, it's not going to make any difference. Variables are just fine in all cases. If you know it's going to never change during the execution then you can define it as a constant, but that really applies more to languages like JAVA.

    If you are new to PHP but know other languages just apply the same methodology - try to keep the reuseable code in one place. You'll learn it quick, PHP is out of all the languages I know the easiest to get a handle on. Good luck, learning new programming skills is always a fun adventure - or maybe I'm just a nerd :)