2009-12-31 62 views
0

魔法__autoload函數僅適用於類,對嗎?如何模板等其他文件?我很想看到一個解決方案,我根本不必關心這個大問題:「文件在哪裏?路徑是什麼?我什麼時候需要包含它?」。會是一個很大的節省時間。有什麼東西可以自動包含PHP以外的類文件嗎?

表現?那麼......在這種情況下,我寧願更快地發展而不是表現,因爲......嘿......小我面對它,99,99%的我們製作的網站很少被訪問。而當我們有一百萬名訪問者的那一天,我們可能是一家大公司,並支付10位開發人員來改進它。

嗯,至少我的框架。

+0

如果你的模板是一個類的話,那就用'__autoload'工作,太。 =) – 2009-12-31 17:00:10

+0

沒有多大意義,使模板類;)rofl ...我有ViewController的,視圖和視圖使用模板。這些只是簡單的「html」文件,最後以.php作爲模板語言。 – openfrog 2009-12-31 17:02:21

回答

5

看看set_include_path()。它允許您設置當您嘗試包含文件時PHP將顯示的目錄列表。所以,如果你有一個目錄中的所有的模板,說templates/,你可以:

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

,PHP將找到正確的文件。這仍然需要include(),但它有幫助。此外,明確你所包含的文件是一件好事。

+0

好的一個,真的很好!謝啦!! – openfrog 2009-12-31 17:05:23

1

你可以去一個更好的。

看在的auto_prepend_file和auto_append_file所php.ini文件。

的auto_prepend_file NULL PHP_INI_PERDIR PHP_INI_ALL在PHP < = 4.2.3。 auto_append_file所NULL PHP_INI_PERDIR PHP_INI_ALL在PHP < = 4.2.3。

的auto_prepend_file串

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所串

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. 
相關問題