PHP - 实例化的类函数调用两次



我试图猜测为什么在我的代码中执行不需要的操作。这是我的代码:

索引.php

include_once("MyClass.php");
new MyClass();

我的班级.php

class MyClass{
     private $LOG;
     function __construct(){
        require_once("Initializer.php");
        $initializer = new Initializer();
        $this->LOG = $initializer->log;
        //Calling the function that performs twice
        $this->LOG->log("test");
     }
}

初始值设定项.php

class Initializer{
     public $log;
     function __construct(){
          require_once("Log.php");
          $this->log = new Log();
     }
}

日志.php

class Log{
     function __construct(){
           echo "This is not displaying twice";
     }
     //This function is performing twice
     public function log($msg){
        $logFile = fopen(TARGET_FILE, "a+");
        fwrite($logFile, $msg . "n");
        fclose($logFile);
     } 
}

我得到的输出是TARGET_FILE

test
test

每次我执行index.php.

问题出在哪里?提前谢谢。

它与PHP无关。我检查了我的.htaccess文件,其中包含以下规则:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+) index.php [L]

我删除了RewriteCond %{REQUEST_FILENAME} !-d,它按预期工作。

最新更新