sushil bharwani
sushil bharwani

Reputation: 30187

Error in php code I have written unable to understand how to correct it?

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

I have created a logging file and i am getting an error in last line

Logging::fp = fopen(....)
the error is unexpected '=' can somebody guide me in understanding and rectifying the error.

Upvotes: 0

Views: 76

Answers (3)

Alexander Larikov
Alexander Larikov

Reputation: 2339

You can use getters/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;
    }
}  

Upvotes: 0

Frank He
Frank He

Reputation: 546

Missing $: Logging::$fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");

Upvotes: 0

Andrew M
Andrew M

Reputation: 4288

The double colon indicates a static property of the class. You cannot assign values to static properties of a class. For more on static properties, see :

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

Upvotes: 0

Related Questions