PHP Fatal error: Cannot access empty property

PhpFatal Error

Php Problem Overview


I'm new to php and I have executed below code.

<?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 below errors. What could be the error?

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

Php Solutions


Solution 1 - Php

You access the property in the wrong way. With the $this->$my_value = .. syntax, you set the property with the name of the value in $my_value. What you want is $this->my_value = ..

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

is the same as

$this->my_value = "test";

To fix a few things from your example, the code below is a better 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 won't change it's type to string or something else when you call set_value. But you can still set the value of my_value direct, because it's public. The final step is, to make my_value private and only access my_value over getter/setter methods

Solution 2 - Php

First, don't declare variables using var, but

public $my_value;

Then you can access it using

$this->my_value;

and not

$this->$my_value;

Solution 3 - Php

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

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

Please read the doc here.

Solution 4 - Php

As I see in your code, it seems you are following an old documentation/tutorial about OOP in PHP based on PHP4 (OOP wasn't supported but adapted somehow to be used in a simple ways), since PHP5 an official support was added and the notation has been changed from what it was.

Please see this code review 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 it helps

Solution 5 - Php

Interesting:

  1. You declared an array var $my_value = array();
  2. Pushed value into it $a->my_value[] = 'b';
  3. Assigned a string to variable. (so it is no more array) $a->set_value ('c');
  4. Tried to push a value into array, that does not exist anymore. (it's string) $a->my_class('d');

And your foreach wont work anymore.

Solution 6 - Php

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 reach it like:

echo $a->foo;  //returns bar

Solution 7 - Php

I realise this answer is not a direct response 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 worth posting my experience here just in case anybody is muddling over the same thing...

You can encounter the error in question as a result of a poorly formatted for loop over an associative array. In a fit of bone-headedness, I was using -> instead of => in my for statement:

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

Of course, I should have had:

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

I confused myself at first, thinking the reported error was referring to the someArray property!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBishanView Question on Stackoverflow
Solution 1 - PhpPhilippView Answer on Stackoverflow
Solution 2 - PhpMarko DView Answer on Stackoverflow
Solution 3 - PhpValView Answer on Stackoverflow
Solution 4 - PhpAbu RomaïssaeView Answer on Stackoverflow
Solution 5 - PhpsybearView Answer on Stackoverflow
Solution 6 - PhpgazdagergoView Answer on Stackoverflow
Solution 7 - PhpJohn RixView Answer on Stackoverflow