Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for including PHP files? [duplicate]

Tags:

include

php

What is the best practice for including PHP files?

Is it best to include a include.php file that includes all of the project PHP files? Or to include it in those files that need them

Right now, my project has several include files in my index.php file. Does including all of my php files in the index.php make it less efficient?

Lastly, Where should one include the session check PHP file? in all of the PHP files?

like image 484
SupaOden Avatar asked Dec 02 '11 09:12

SupaOden


People also ask

How to Duplicate file PHP?

The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.

What are the two methods to include files in PHP?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

Can we include one PHP file multiple times?

If the file is included twice, PHP will raise a fatal error because the functions were already declared. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file.

Where should I store PHP files?

If your server supports PHP, then you do not need to do anything. Just create your . php files, put them in your web directory and the server will automatically parse them for you.


1 Answers

EDIT 2016

Well, 5 years since I replied this. I am still alive. A lot has changed.

Now I use autoloaders to include my files. Here is official info for autoloaders with examples.

Basically, the idea is to have a proper folder structure (PSR-4 standards for instance) and having a class within each file. This way you can use autoloaders and PHP will load your files automatically.

OLD ANSWER

Usually, I have a config file like this:

define(root, $_SERVER['DOCUMENT_ROOT']);
.... // other variables that are used a lot

include (root . '/class/database.php'); 
.... // other includes that are mostly called from each file, like a db class, or user class, functions etc etc...

if (defined('development'))
{
    // turn error reporting on
}
else 
{
    // turn it off
} 

etc etc... You got the point of config.

And I include the config.php on each file. I forgot how to do it right now, but apache can do the automatic include for you. Therefore, you can say to apache to include your config file by default.

Then, I have controller classes, which call the views. There in each function I call the view.

someController.php

function index() { include root . '/views/view_index.php'; }

finally, from the view, if I need to include the header and footer view I do it like this:

view_index.php

<?include root . '/view/shared/header.php';?>
<div class="bla bla bla">bla bla bla</div>
<?include root . '/view/shared/footer.php';?>

I always use include in this structure rather than include_once since the latter requires extra check. I mean, since I am pretty sure that I include files only once, I don't need to use include_once. This way, you also know which include is where. For instance, you know that crucial files like db.php, or functions.php are located in config.php. Or you know that include views are located in controllers. That's pretty useful for me, I hope that helps you, too.

like image 165
Savas Vedova Avatar answered Oct 12 '22 23:10

Savas Vedova