[英]How can I prevent the same file being included twice when using hardlinks?
如何防止同一文件被包含兩次? 這是我的源代碼:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
if (!file_exists('ccc.php'))
link('bbb.php', 'ccc.php');
if (!file_exists('ddd.php'))
link('bbb.php', 'ddd.php');
require_once realpath('ccc.php');
require_once realpath('ddd.php');
$bbb = new Bbb();
echo $bbb->bb();
我收到:
Fatal error: Cannot declare class Bbb, because the name is already in use in /path/to/ddd.php on line 2
它不適用於realpath
因為它只返回鏈接的路徑,而不是目標。 我試過readlink
但不幸的是它只適用於符號鏈接。
我有一個可行的解決方案。 這並不理想,但它可以快速修復,直到我能夠確保永遠不會包含相同的文件兩次:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
if (!file_exists('ccc.php'))
link('bbb.php', 'ccc.php');
if (!file_exists('ddd.php'))
link('bbb.php', 'ddd.php');
function includeFile($fileName)
{
foreach (get_included_files() as $file)
{
if (fileinode($file) === fileinode($fileName))
return null;
}
require_once $fileName;
}
includeFile('ccc.php');
includeFile('ddd.php');
$bbb = new Bbb();
echo $bbb->bb();
感謝BugFinder建議我使用fileinode
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.