PHP代码出错我写的无法理解如何纠正它?

时间:2012-07-25 05:20:32

标签: php oop

 class Logging{   

    private $log_file = 'c:/xampp/htdocs/jcert2/tmp/sslogfile.txt';   
    public  static $fp = null;

    public static function lwrite($message){   
    if (Logging::fp) Logging::lopen();   
  //  $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);   
    $time = date('H:i:s');   
    fwrite(Logging::fp, "$time $message\n");   
  }   
  // open log file   
  private static function lopen(){   
    $lfile = $this->log_file;   
    $today = date('Y-m-d');   
    Logging::fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");   
  }   
}  

我创建了一个日志文件,我在最后一行收到错误

Logging::fp = fopen(....)
错误是意外的'='有人可以指导我理解和纠正错误。

3 个答案:

答案 0 :(得分:0)

双冒号表示类的静态属性。您不能将值分配给类的静态属性。有关静态属性的更多信息,请参阅:

http://php.net/manual/en/language.oop5.static.php

答案 1 :(得分:0)

缺少$: 记录:: $ fp = fopen($ file。'_'。$ today,'a')或退出(“无法打开$ file!”);

答案 2 :(得分:0)

您可以使用getter / setters

class Logging{   

    private $log_file = 'c:/xampp/htdocs/jcert2/tmp/sslogfile.txt';   
    private $fp = null;

    private static function lopen(){   
        $lfile = $this->log_file;   
        $today = date('Y-m-d');   
        $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");   
    }

    public static function get_fp(){
        return $this->fp;
    }
}