开发者

Is there something to auto-include files other than classes in PHP?

开发者 https://www.devze.com 2022-12-15 08:38 出处:网络
The magic __autoload function works for classes only, right? How about other files like templates? I\'d love to see a solution where I don\'t have to care at all about the big problem \"where\'s the f

The magic __autoload function works for classes only, right? How about other files like templates? I'd love to see a solution where I don't have to care at all about the big problem "where's the file? what's the path? when do I have to include it?". Would be a big time saver.

Performa开发者_高级运维nce? Well... in this case I'd prefer faster development over performance, because...hey..lets face it, 99,99% of our websites we make are rarely visited anyways. And when the day comes where we get a million visitors, we're probably a big, Inc. and pay 10 devs to improve it.

Well, at least for my framework.


Take a look set_include_path(). It allows you to set a list of a directories in which PHP will look when you try to include a file. So if you have all your templates in one dir, say templates/, you can just:

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

And PHP will find the right file. This still requires an include(), but it helps. Besides, being explicit about which files you include is a Good Thing.


You can go one better.

look in the php.ini file for auto_prepend_file and auto_append_file.

auto_prepend_file NULL PHP_INI_PERDIR PHP_INI_ALL in PHP <= 4.2.3. auto_append_file NULL PHP_INI_PERDIR PHP_INI_ALL in PHP <= 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. 
0

精彩评论

暂无评论...
验证码 换一张
取 消