PHP Fatal error: cannot access empty property

I am new to php and I have executed the code below.

 <?php class my_class{ var $my_value = array(); function my_class ($value){ $this->my_value[] = $value; } function set_value ($value){ // Error occurred from here as Undefined variable: my_value $this->$my_value = $value; } } $a = new my_class ('a'); $a->my_value[] = 'b'; $a->set_value ('c'); $a->my_class('d'); foreach ($a->my_value as &$value) { echo $value; } ?> 

I got errors. What could be a mistake?

 Notice: Undefined variable: my_value in C:\xampp\htdocs\MyTestPages\f.php on line 15 Fatal error: Cannot access empty property in C:\xampp\htdocs\MyTestPages\f.php on line 15 
+48
php fatal-error
Feb 17 '13 at
source share
7 answers

You are accessing the property incorrectly. With the syntax $this->$my_value = .. you set the property with the value name to $ my_value. Do you want $this->my_value = ..

 $var = "my_value"; $this->$var = "test"; 

coincides with

 $this->my_value = "test"; 

To fix a few things from your example, the code below is better than aproach

 class my_class { public $my_value = array(); function __construct ($value) { $this->my_value[] = $value; } function set_value ($value) { if (!is_array($value)) { throw new Exception("Illegal argument"); } $this->my_value = $value; } function add_value($value) { $this->my_value = $value; } } $a = new my_class ('a'); $a->my_value[] = 'b'; $a->add_value('c'); $a->set_value(array('d')); 

This ensures that my_value does not change its type to a string or anything else when you call set_value. But you can still set my_value direct because it is open. Last step: make my_value private and only access my_value using getter / setter methods

+150
Feb 17 '13 at 10:41
source share

First, do not declare variables with var, but

 public $my_value; 

Then you can access it using

 $this->my_value; 

but not

 $this->$my_value; 
+24
Feb 17 '13 at 10:41
source share

To access a variable in a class, you must use $this->myVar instead of $this->$myvar .

And you should use an access identifier to declare a variable instead of var .

Please read the document here .

+9
Feb 17 '13 at 10:41
source share

As I see in your code, it seems that you are following the old documentation / tutorial on OOP in PHP based on PHP4 (OOP was not supported, but adapted somehow for use in a simple way), since PHP5 is official support and the notation has been changed from of what was.

Please view this code here:

 <?php class my_class{ public $my_value = array(); function __construct( $value ) { // the constructor name is __construct instead of the class name $this->my_value[] = $value; } function set_value ($value){ // Error occurred from here as Undefined variable: my_value $this->my_value = $value; // remove the $ sign } } $a = new my_class ('a'); $a->my_value[] = 'b'; $a->set_value ('c'); // your array variable here will be replaced by a simple string // $a->my_class('d'); // you can call this if you mean calling the contructor // at this stage you can't loop on the variable since it have been replaced by a simple string ('c') foreach ($a->my_value as &$value) { // look for foreach samples to know how to use it well echo $value; } ?> 

I hope this helps

+5
Feb 17 '13 at 10:51
source share

Interesting:

  • You declared an array var $my_value = array();
  • The embedded value in it is $a->my_value[] = 'b';
  • Assigning a variable to a variable. (so this is no longer an array) $a->set_value ('c');
  • Tried to push the value to an array that no longer exists. (this is a string) $a->my_class('d');

And your foreach will no longer work.

+1
Feb 17 '13 at 10:46
source share

I understand that this answer is not a direct answer to the problem described by the OP, but I found this question as a result of searching for the same error message. I thought it was worth publishing my experience here, just in case someone gets confused over the same ...

You may encounter this error as a result of a poorly formatted for loop over an associative array. In a bone-head fit, I used → instead of => in my for statement:

  foreach ($object->someArray as $key->$val) { // do something } 

Of course I had to:

  foreach ($object->someArray as $key=>$val) { // do something } 

At first I got confused thinking that the error reported was related to someArray property!

0
Oct 21 '15 at 21:21
source share

This way you can create a new object with a custom property name.

 $my_property = 'foo'; $value = 'bar'; $a = (object) array($my_property => $value); 

Now you can achieve it as:

 echo $a->foo; //returns bar 
0
Oct 30 '15 at 10:41
source share



All Articles