Jump to content

Class problem


Matpatnik

Recommended Posts

Hi guys, I have this error when using this function generateNumber() it tell me Fatal error: Cannot access empty property.can someone enlighten me plz

<?phpclass generateLotto {	public $stringNumber;	public $maxNumber;	public $repeatNumber;		// doesn't work	function __constructor($maxNum=49, $repeat=5) {		$this->maxNumber = $maxNum;		$this->repeatNumber = $repeat;		$this->stringNumber = '';	}		// if __constructor is not set	function generateLotto($maxNum=49, $repeat=5) {		$this->maxNumber = $maxNum;		$this->repeatNumber = $repeat;		$this->stringNumber = '';		echo $this->maxNumber .' - '. $this->repeatNumber; // this work fine	}		function getRandomNumber() {		// chose a number between 1 and $maxNum		$this->stringNumber = mt_rand(1, $this->maxNumber); // this work fine	}		function generateNumber() {		echo '-'. $this->$repeatNumber .'-'; // problem is here: Fatal error: Cannot access empty property		for ($i=0; $i<$this->$repeatNumber; $i++) {			$this->getRandomNumber();			if (in_array($this->stringNumber, $lotto)) {				continue;			}			$lotto[$i] = $this->stringNumber;			echo $lotto[$i];		}		echo $lotto;		$this->stringNumber = implode('-', $lotto);	}		function showNumber() {  // this work fine		//$this->getRandomNumber();		return $this->stringNumber;	}}$LOTTOnumber = new generateLotto(50,3);$LOTTOnumber->generateNumber();echo ' -> '. $LOTTOnumber->showNumber();die();?>

Link to comment
Share on other sites

$this->$repeatNumber

and

$this->repeatNumber

are two different things. The first is not a parsing error, which is why the rest still works. It would get the property that has the same name as the value of the $repeatNumber variable (that is in the current scope).The second is what you need. That will get the repeatNumber property.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...