What is the best practice for including PHP files?

What is the best practice for including PHP files?

Is it better to include an include.php file that includes all the PHP project files? Or include it in the files that they need.

Now my project has several include files in my index.php file. Does all my php files in index.php include less efficient ones?

Finally, where to include the PHP verification session file? in all PHP files?

+4
source share
5 answers

EDIT 2016

Okay, 5 years since I answered. I am still alive. A lot has changed.

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

Basically, the idea is to have the correct folder structure (e.g. PSR-4 standards) and have a class in each file. This way you can use autoloaders, and PHP will automatically download your files.

OLD ANSWER

I usually have a configuration 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 config.php in every file. I forgot how to do it right now, but apache can automatically turn you on. Therefore, you can tell apache to include your default configuration file.

Then I have controller classes called views. In each function, I call a view.

someController.php

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

Finally, from the view, if I need to include a header and footer, 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, not include_once , since the latter requires additional verification. I mean, since I'm sure that I only include files once, I don't need to use include_once . That way, you also know where to include it. For example, you know that important files, such as db.php or functions.php, are in the config.php file. Or you know that included views are in controllers. This is very useful for me, I hope this helps too.

+10
source

Using the include.php file is a very good practice, as it is very useful when replacing included files in large projects. If the project is small, then including individual files is not a problem. But it becomes a problem to manage them as the project grows.
For a session verification file, it is better to attach them separately, since the requirement for session verification on different pages may differ.
Including files individually or including all of them in one file, and then includes what is of great importance for performance. As a result, all files will be included. It becomes easier to manage them if a single file is used to process them.

0
source

Just think lightly and think about downloading as little as possible and don't turn on something unnecessary.

So, for your PHP files. if you include the same php files on different pages, just create 1 php file with these files in it. If you are using a PHP file on only 1 page or 2, just include them separately.

Hope I helped you with that;)

0
source

I do not assume that you are using object-oriented programming, but in case you are here, this might be a good answer.

In php, you can define a function called autoloader, if you try to create an object of a class that has not been defined, the autoloader is called. You can then use the class name to find out where the file containing this class is stored, so you can include it at the last moment. Here is an example.

 <?php function on_load($class) { if(file_exists(require_once('classes/'.$class.'.php'))) { require_once('classes/'.$class.'.php'); } else { throw new Exception('Class not found: '.$class.' in classes/'); } } spl_autoload_register('on_load'); // tell php to call your on_load function if the class was not defined 

If you are working on a large project, you may want to group your files as follows

 /classes/database/MySQL.php /classes/database/PDO.php // I'm just listing random stuff /classes/Core.php // Whatever /classes/datastructure/HashMap.php 

Then you can use a special naming convention to find the correct directory

 class Database_MySQL{} // look in <root_dir>/database/ for MySQL.php class Core // look in <root_dir>/ for Core.php class Database_Driver_Adapter_Useless_MysqlAdapterThingy {} // look in <root_dir>/Database/Driver/... blabla 

Or you can use php 5.3 method and define your classes as follows

 <?php namespace database\driver\adapter\useless; use database\driver\adapter\MysqlAdapter; // Now you have to tell PHP which version of MysqlAdapter class you want to use, even if there is just one class MysqlAdapterThingy extends MysqlAdapter {} 

Now you need to use the 'use' keyword in every file you need. The best part is that the namespace is automatically added to the class name for your autoload function, so you can do something like this

 function on_load($class) { require_once('root/' . str_replace('\\', '/' $class)); } 

If you want to know more, try googeling the PHP download, there are tons of information on this. But then again. From the format of your question, I do not assume that you are using OOP, so this answer is only for people who found this question on Google.


Edit

I would also like to add the following:

  // Only include the file once even if you place this statement multiple times require_once('bla.php'); include_once('bla.php'); require('bla.php'); // Error if file doesn't exist, php will not continue inlcude('bla.php'); // Warning if file doesn't exist, but php will continue 
  • Using include or require without _once means the file will be included every time the statement is executed
  • Use include for templates or user-created files, use require_once for classes
0
source

Include files independently, use the require_once() function instead of include , since require_once allow only one inclusion of the file ...

-1
source

Source: https://habr.com/ru/post/1384289/


All Articles