PHP需要文件策略 [英] PHP include file strategy needed

查看:62
本文介绍了PHP需要文件策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个php项目,但是对如何正确使用php的include/require命令不是很熟悉.我的布局目前看起来像这样:

I'm in the process of setting up a php project, but am not very familiar with how to properly use php's include/require commands. My layout currently looks like this:

/public   --apache points into this directory  
/public/index.php  
/public/blah/page.php  
/utils/util1.php       -- useful classes/code are stored in other directories out here  
/dbaccess/db1.php  

dbaccess/db1.php  



require '../utils/util1.php

public/index.php

public/index.php

require '../dbaccess/db1.php'

public/blah/page.php

public/blah/page.php

require '../../dbaccess/db1.php'

问题是来自php'include'文档的

The problem is this from the php 'include' documentation:

如果文件名以./或../开头,则仅在当前工作目录中查找

If filename begins with ./ or ../, it is looked only in the current working directory

所以public/blah/page.php失败,因为它包含dbaccess/db1.php,当它尝试包含util1.php时它会爆炸.它失败,因为它的相对路径来自public/blah/中的原始脚本,而不是来自dbaccess/

So public/blah/page.php fails because it includes dbaccess/db1.php which blows up when it tries to include util1.php. It fails because it's relative path is from the original script in public/blah/, not from dbaccess/

这看起来很愚蠢-db1.php必须只知道其中包含的内容,而哪些地方将不起作用.

This seems pretty stupid -- db1.php has to just know where it's being included from which isn't going to work.

我看过这样的策略:

require_once dirname(__FILE__) . '/../utils/util1.php');

这显然是可行的,因为现在这条路是一条绝对的路,但对我来说似乎真的很奇怪.

That apparently works since now the path is an absolute path, but just seems really bizarre to me.

那正常吗?我应该继续沿着那条路走还是在这里错过明显的东西?

Is that normal? Should I continue down that path or am I missing something obvious here?

推荐答案

通常,标准约定如下:就像@grepsedawk所说的那样,您将需要定义一个包含项目文件夹根目录的常量,如果可以的话您的include文件夹的根目录:

Usually, the standard conventions are thus: like @grepsedawk said, you'll want to define a constant that contains the root of your project folder and if you can the root of your includes folder:

define('APP_ROOT', dirname(__FILE__));
define('INCLUDE_ROOT', APP_ROOT . "/includes");

注意:常量名必须是字符串!

Note: the constant name needs to be a string!

此外,您还会注意到我正在使用dirname(__FILE__);.如果将常量定义文件放在子目录中,则可以执行dirname(dirname(__FILE__));,它等效于../.

Also, you'll notice I'm using dirname(__FILE__);. If you place your constants definition file in a subdirectory, you can do a dirname(dirname(__FILE__));, which is the equivalent of a ../.

现在有一些其他警告.虽然PATH_SEPARATOR是一个很酷的常数,但它不是必需的. Windows接受路径名中的/或\,并且由于Linux仅将用户/作为路径分隔符,因此请继续使用/而不是重复引用PATH_SEPARATOR来破坏代码.

Now some other caveats. While PATH_SEPARATOR is a cool constant, it is not needed. Windows accepts / or \ in path names, and since Linux only users / as a path separator, go ahead and always use a / instead of mucking up your code with repeated references to PATH_SEPARATOR.

现在,您已经定义了根常量,在需要包含配置文件时要做的事情很简单:

Now that you have your root constants defined, what you'll do when you need a configuration file included is a simple:

include INCLUDE_ROOT . '/path/to/some/file.php';

您可能希望在根目录的引导脚本中使用常量定义(上面的define(...)):

You'll probably want your constant definitions (the define(...)'s above) in a bootstrap script in your root directory:

www_root/
  index.php
  bootstrap.php

引导程序将包含定义(或常量文件的include)以及每个页面都需要的任何文件的include.

The bootstrap will contain the defines (or an include of the constants file), as well as an include of any files that will be required by EVERY page.

最后,您可能不会使用的最后一个标准约定,但是如果您开始进行面向对象的编程,则最常见的方法(PEAR标准)是通过使用_分隔命名空间来命名类:

And finally the last standard convention you may not use, but if you start doing object oriented programming, the most common method (the PEAR standard) is to name your classes by using an _ to separate namespaces:

class GlobalNamespace_Namespace_Class
//...

然后组织将名称空间映射到子目录的文件结构(将所有_替换为/):

And then organizing your file structure mapping name spaces to subdirectories (literally replacing all _'s with /'s):

include_dir/
  GlobalNamespace/
      Namespace/
          Class.php

并使用__autoload()函数加载类,但这是另一个问题.

And using __autoload() functions to load your classes, but that's another question.

这篇关于PHP需要文件策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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