assigning rand() to a variable within a class and declaring it private

THEROMPSTER2000
THEROMPSTER2000 used Ask the Experts™
on
I am tryng to assign rand() to variable $random as a private variable in a php class and i keep getting parse error here, when i assign it the same exact way outside of the declarations its fine.
<?php
 
 
class phplog
{
	private $gMaxFileCount = 2;
  	private $gMaxSize = 200; // 200KB
	private $gFileName = "";
	private $gFilePath = "";
	private $random = rand();
//EXTERNAL Constructor
	function phplog($pFileName,$pFilePath)
	{	
		$this->gFileName = $pFileName;
		$this->gFilePath = $pFilePath;
		exec("dir /o:d /b" . rtrim($pFilePath,"/") . "/" . $pFileName . "????????????.txt /b",$filelist);
 
	//Remove old files
		if(count($filelist) > $this->gMaxFileCount)
		{
			for ($i=1;$i<=count($filelist) - $this->gMaxFileCount;$i++)
			{
				unlink("$filelist[$i]");
			}
		}
 
	//Make a new file if the log is > max size
		if(filesize($pFileName) >= $this->gMaxSize)
  		{
  	  		rename($pFileName, $pFileName.time() . ".txt");
  		}
	}  //phplog
 
//EXTERNAL Writes to the existing log
	function WriteToLog($message)
	{
		file_put_contents($this->gFileName, date("YdmHis",time()) . " " . $random . str_replace("\n","",$message) . "\n", FILE_APPEND);	
		/*
  		$f = fopen($pFileName, "a+") or die("Unable to open log file");
  		fwrite($f, $message);
  		fclose($f);*/
	}
}
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You cannot call a function in the declaration section of a class. You must assign it in the constructor.
<?php
 
 
class phplog
{
	private $gMaxFileCount = 2;
  	private $gMaxSize = 200; // 200KB
	private $gFileName = "";
	private $gFilePath = "";
	private $random = 0;
//EXTERNAL Constructor
	function phplog($pFileName,$pFilePath)
	{	
                $this->random = rand();
		$this->gFileName = $pFileName;
		$this->gFilePath = $pFilePath;
		exec("dir /o:d /b" . rtrim($pFilePath,"/") . "/" . $pFileName . "????????????.txt /b",$filelist);
 
	//Remove old files
		if(count($filelist) > $this->gMaxFileCount)
		{
			for ($i=1;$i<=count($filelist) - $this->gMaxFileCount;$i++)
			{
				unlink("$filelist[$i]");
			}
		}
 
	//Make a new file if the log is > max size
		if(filesize($pFileName) >= $this->gMaxSize)
  		{
  	  		rename($pFileName, $pFileName.time() . ".txt");
  		}
	}  //phplog
 
//EXTERNAL Writes to the existing log
	function WriteToLog($message)
	{
		file_put_contents($this->gFileName, date("YdmHis",time()) . " " . $random . str_replace("\n","",$message) . "\n", FILE_APPEND);	
		/*
  		$f = fopen($pFileName, "a+") or die("Unable to open log file");
  		fwrite($f, $message);
  		fclose($f);*/
	}
}
?>

Open in new window

Trust and believe this will accomplish your goal 100% :)

Author

Commented:
I know, it is correct.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial