PHP Fatal error: Cannot access empty property in


Are you trying to access any object’s property (variables/functions) using: $this->$property ?

Check out the red $ should not appear before object properties. In contrast:

<?php
	class A{
		$b = "some value";

		public function get_b(){
			return $this->$b;	// WRONG !!!
		}
	}

	// Using the class

	$myObject = new A();
	echo $myObject-$get_b();	// WRONG!!!
?>

the above code segment is wrong. You should use $ only once – before ‘ this’ only. No need to use $ sign before class variables or methods.

Correct version of the above code segment:

<?php
	class A{
		$b = "some value";

		public function get_b(){
			return $this->b;
		}
	}

	// Using the class

	$myObject = new A();
	echo $myObject-get_b();
?>
Advertisements

4 thoughts on “PHP Fatal error: Cannot access empty property in

  1. Good job man. Exactly what I needed. I find PHP’s OOP notation a little tricky coming from EMACS.

    1. Vaia amra jara bangali tader jonno ki kichhu kora jay. Jmon dharabahik tutorial ja webdesign, graphics design, HTML, PHP, CSS, Java Script, Jquery, Linux, ubontu, e.t.c aro koto ki oneke achhe jara ghore boshe kichhur chesta kore, Kintu amader DESHER RAJNOITIK NETA KORMI-der moto ashob jara janen tarao taka chhara kichui bujhena, Tader site gulo shudhu comment r reply er jonno noyto TARA JANEN eta JANANOR BIGGAPON. Michhei desh gorobe tara gorib noyto cripon Uvoyta tei deyar shamortho nai.

Comments are closed.