PHP - 实例化 class 函数调用两次
PHP - Instantiated class function calling twice
我正在尝试猜测为什么我的代码中执行了不需要的操作。这是我的代码:
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
.
问题出在哪里?提前致谢。
与PHP无关。我检查了我的 .htaccess
文件,其中包含以下规则:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+) index.php [L]
我删除了 RewriteCond %{REQUEST_FILENAME} !-d
,它按预期工作。
我正在尝试猜测为什么我的代码中执行了不需要的操作。这是我的代码:
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
.
问题出在哪里?提前致谢。
与PHP无关。我检查了我的 .htaccess
文件,其中包含以下规则:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+) index.php [L]
我删除了 RewriteCond %{REQUEST_FILENAME} !-d
,它按预期工作。