包含PHP文件的最佳做法是什么? [英] What is the best practice for including PHP files?

查看:84
本文介绍了包含PHP文件的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含PHP文件的最佳做法是什么?

What is the best practice for including PHP files?

最好包含 include.php 包含所有项目PHP文件的文件?
或者将它包含在那些需要它们的文件中

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

现在,我的项目在我的 index.php中有几个包含文件/ code>文件。在index.php中包含我所有的php文件会降低效率吗?

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?

最后,哪一个应包含会话检查PHP文件?在所有PHP文件中?

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

推荐答案

编辑2016

好吧,我回答这个问题已有5年了。我还活着。很多都改变了。

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.

基本上,我们的想法是拥有适当的文件夹结构(例如PSR-4标准)并在每个文件中都有一个类。这样您就可以使用自动加载器,PHP将自动加载您的文件。

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.

我在每个文件中都包含config.php。我忘了现在该怎么做,但是apache可以自动为你做。因此,默认情况下,您可以说apache包含您的配置文件。

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

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

view_index.php

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

我总是在这个结构中使用 include include_once 因为后者需要额外检查。我的意思是,因为我非常确定我只包含一次文件,所以我不需要使用 include_once 。这样,你也知道哪个包含在哪里。例如,您知道像db.php或functions.php这样的关键文件位于config.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.

这篇关于包含PHP文件的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆