在PHP中有什么东西可以自动包含除了类之外的文件吗?

时间:2009-12-31 16:54:27

标签: php

魔法__autoload函数仅适用于类,对吧?其他文件如模板怎么样?我很乐意看到一个解决方案,我根本无需关心这个大问题“文件在哪里?路径是什么?我什么时候必须包含它?”。将节省大量时间。

性能?嗯......在这种情况下,我更喜欢快速开发而不是性能,因为......嘿......小时候,我们制作的99,99%的网站很少会被访问。当我们获得一百万访客的那一天,我们可能是一个大公司,并支付10个开发人员来改善它。

好吧,至少对我的框架而言。

2 个答案:

答案 0 :(得分:5)

看看set_include_path()。它允许您设置当您尝试包含文件时PHP将在其中查找的目录列表。因此,如果您在一个目录中拥有所有模板,请说templates/,您可以:

set_include_path(get_include_path() . PATH_SEPARATOR . 'templates');
//...
include 'mytemplate.php';

PHP会找到合适的文件。这仍然需要include(),但它有所帮助。此外,明确你所包含的文件是一件好事。

答案 1 :(得分:1)

你可以做得更好。

在php.ini文件中查找auto_prepend_file和auto_append_file。

auto_prepend_file PHP PHP_INI_PERDIR PHP_INI_ALL,PHP <= 4.2.3。 auto_append_file PHP PHP_INI_PERDIR PHP_INI_ALL in PHP&lt; = 4.2.3。

auto_prepend_file string

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require() function, so include_path is used.

The special value none disables auto-prepending.

auto_append_file string

Specifies the name of a file that is automatically parsed after the main file. The file is included as if it was called with the require() function, so include_path is used.

The special value none disables auto-appending.

    Note: If the script is terminated with exit(), auto-append will not occur.