66

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
2
  • 11
    If you're just learning PHP, look for some more up-to-date tutorials... the use of var and a method with the same name as the class indicate a pretty old, outdated tutorial
    – Mark Baker
    Feb 17, 2013 at 10:42
  • @MarkBaker thanks for your instructions.
    – Bishan
    Feb 17, 2013 at 10:47

7 Answers 7

181

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

2
  • Then i'm getting error saying Fatal error: [] operator not supported for strings in C:\xampp\htdocs\MyTestPages\f.php on line 12. Line 12 is $this->my_value[] = $value;
    – Bishan
    Feb 17, 2013 at 10:45
  • Sure.. set_value change the type from my_value from array to string. Probably you want to change the code inside set_value to $this->my_value[] = $value;
    – Philipp
    Feb 17, 2013 at 10:48
32

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;
10

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.

2
  • Then i'm getting error saying Fatal error: [] operator not supported for strings in C:\xampp\htdocs\MyTestPages\f.php on line 12. Line 12 is $this->my_value[] = $value;
    – Bishan
    Feb 17, 2013 at 10:45
  • You can't call $this->my_value[] you should have a value in the [].
    – Val
    Feb 17, 2013 at 16:31
5

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

1

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.

1

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
1
  • would have been more detailed..!
    – Chella
    Oct 30, 2015 at 10:47
0

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!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.