My usual approach is to create an autoloader function to pull classes as needed, and the class to handle the database connection. However, this may be a bit overwhelming for some projects. Also, this configuration has yet to be included before my code, which calls the class and expects something to go and get.
Many open source PHP programs will set such values โโin a configuration file that will be included very early along with any libraries, etc.
For example (LM), NucleusCMS uses a configuration file to load global functions and other core classes, and so all that is needed is for the PHP script to make sure that config.php is enabled.
Sometimes the code you are viewing is intended to be included at runtime with other code, and therefore believes that these settings are already set. For example, I am currently working on a project that redirects all calls to PHP files to start.php, which does what is necessary, and then calls the file after that, and then finally calls the template to process the output.
If you really want to not include it at all, then there is another option, but it is not recommended. You can set the auto_prepend_file php.ini directive and include the file in EVERY script that the server runs. As I said, you probably don't want this.
However, if you want the script to handle inclusion for you, and this is important, then using the .htaccess and start.php example (actually ./engine/start.php), then this might work for you better than spoofing directives ini. However, the disadvantage is that you need to check that your .htaccess redirects work correctly by passing the file name back to start.php, and then make sure some naughty user is not asking your script to do something bad. After you have cleared and sanity checked the input, you can go ahead and require_once ($ filename) ...
My .htaccess looks something like this:
RewriteEngine on RewriteRule ^(.*).php?$ ./path/to/start.php?page=$1 [QSA,L]
However, as I said, if you donโt have a specific need to do this, then it would be safer if you allow ANY input in the include / require line and have to spend a lot of resources on EVERY page load, make sure the request is safe.
To be honest, the most common practice is to have a file called common.php, and everything that includes scripts to get there, and just
require_once('./path/to/common.php');
at the top of each page. If your project ends with a large number of classes and / or libraries in it, then you may be glad that you did it.