2017-04-11 79 views
6

有數以百計的地方在我的項目包含以下代碼:通過連接另一個字符串覆蓋一個字符串

include $root . $template; 

哪裏$root對於CRM和$template相關的部分文檔根目錄的HTML模板文件。對於CRM的不同部分,$root變量是不同的。

我想現在讓CRM的所有部分使用相同的模板文件。我不能簡單地將$root更改爲中央文檔根目錄,因爲代碼中有其他地方使用該變量。我只是想改變它來加載模板。

有沒有辦法節省時間,所以我不必在我的代碼中出現所有這些地方的地方?當concatinated時,是否有可能使$template字符串覆蓋$root字符串?

$root="$_SERVER['document_root']."\some_folder"; 
$template="[something_to_ignore_concatination?]".$_SERVER['document_root']."\template_file"; 
//So $root.$template = just $template 
+0

首先對'$ root'使用一個常量,它不會改變,所以dosomething就像'define('APPROOT','/ foo/bar')'。然後使這個'$ template = APPROOT。「/ rest/of/the/path」' – JustOnUnderMillions

+0

爲什麼不在你的文本編輯器中進行全局查找和替換?找到'include $ root。 $ template;'並用'include $ template;'替換。崇高的文本3絕對有這種能力,例如 – Spholt

+0

你是假設這是可能的,還是你真的會使用這樣的解決方案? –

回答

3

警告:你會看到什麼下面是可憎的,不要在真正的軟件使用此功能!

假設這些值:

$_SERVER['document_root'] = 'path/to/root' 

$root == $_SERVER['document_root'] . '/some_folder' == 'path/to/root/some_folder' 

$template == '/' . $_SERVER['document_root'] . '/file.php' == '/path/to/root/file.php' 

$root . $template == 'path/to/root/some_folder/path/to/root/file.php' 

你可以符號鏈接

path/to/root/some_folder/path/to/root -> path/to/root/ 

因此,雙路徑仍然得到解決。

注意:上面的路徑應視爲相對於添加到PHP include_path的文件夾之一。

+0

非常有創意,謝謝你的解決方案。你說得對,我應該搜索並更換爲最佳做法。 – dlofrodloh

相關問題