PHPクラスを作成していますが、スコープの定義にかなりの問題があります。SOに関するこの概念に関する記事をたくさん読んだのですが、コードの問題が何であるかを判断できないようです。
class Logger {
private static $logger ;
private $res ;
private $file ;
private $mode ;
public static function getInstance() {
if (!self::$logger) $instance = new self() ;
self::$logger = $instance ;
return self::$logger ;
}
private function initializeLogger( ) {
$this->file = '/tmp/mydirectory/mylog.log' ;
$this->res = fopen($this->file, 'a') or exit("Can't open ".$this->file);
}
public function write( $message , $modeLevel ) {
if ( !is_resource($this->res )) {
$this->initializeLogger( ) ;
}
fwrite($this->res, "$message" . PHP_EOL);
}
public function close()
{
fclose(self::$logger);
}
}
$log = Logger::getInstance();
$log.write( "WOW, it's working!!" , 1 );
このコードを実行すると、次のように生成されます。/var/www/myfile.phpの未定義関数write()の呼び出し
非静的な方法で参照できるが、