PHP include best practices question

Viewed 21816

I have been learning syntax for PHP and practicing it. I come from a .NET background so masterpages always made things pretty easy for me when it came to headers and footers.

So far I have a mainHeader.php and mainFooter.php which have my head menu and my footer html. I created a mainBody.php and at the top I put

<?php include "mainHeader.php" ?>

and for the footer I put

<?php include "mainFooter.php" ?>

This worked perfectly and made me smile because my pages all came together nicely. the mainHeader has my <html> and <body> and my mainFooter has my closing tags for those.

Is this good practice?

10 Answers

The accepted answer is form 2010 and things have changed in the past ten years.

The way to go, now with composer having replaced most hand wired autoloaders, the best practice is to use a single require_once utilizing __DIR__ from a script in a fixed, known place:

require_once __DIR__ . '/vendor/autoload.php';

Using define() is not common anymore.

According to the environment agnostic approach, dependencies from the environment get injected to the application using .env or similiar.

Related