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

时间:2017-10-12 12:21:02

标签: php class fwrite instances

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

的index.php

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

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");

     }

}

Initializer.php

class Initializer{

     public $log;

     function __construct(){

          require_once("Log.php");
          $this->log = new Log();

     }

}

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

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

1 个答案:

答案 0 :(得分:0)

它与PHP无关。我检查了具有此规则的.htaccess文件:

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

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