Daniel Skarbek
Daniel Skarbek

Reputation: 574

PHP: Illegal string offset because [] binds tighter than ->

I am fairly new to PHP and just had a learning experience that I am sharing here to help others who, like me, may need help to find the cause of this error and also because I still don't know what the solution is and am sure there is simply a syntax that I just haven't found yet to do what I need to do.

So, the problem can be demonstrated with something like this:

class Sample {
    protected $foo = array();
    public function magicSampleSetFunc($property, $key, $value) {
        $this->$property[$key] = $value;
    }
}
...
$s = new Sample();
$s->magicSampleSetFunc('foo', 'someKey', 'someVal');

Obviously, this isn't my real code (nor have I run it) this is just a minimal example to explain my situation. Here we have a member variable foo that is clearly an array and a generic function that is going to try to set a key and value into it. I can var_dump $this->$property and see that it is an array, but on the $this->$property[$key] line I get the error message: "Warning: Illegal string offset 'someKey' in ...".

At first I though it was saying that 'someKey' was an illegal string to use as an array offset, which didn't make sense. And, even if I wrap it in an isset it complains. The first thing I learned is that if you have a string in php, you can use the array access operator to get a character out of that string. So the warning message is actually complaining that 'someKey' is not a valid offset into a string (because it is not an integer offset). Okay, but I just var_dumped $this->$property and see that it is an array, so what gives? The second lesson was one of operator precedence. The array operator "[]" binds tighter than the indirection operator "->". So, the binding of that statement is actually something like: ( $this-> ( $property[$key] ) ). So, it is illegally trying to offset the string in $property by the index in $key. What I wanted was to offset the array in $this->$property by the index in $key.

So, now we come to my question. The third lesson I need to learn, and haven't figured out yet is how do I override this operator precedence issue? I tried ($this->$property)[$key] but that appears to be a sytax error. Is there some other syntax I can use to get the interpreter to understand what I meant to do? Or, do I have to assign $this->$property to a temporary variable first? If I do, wouldn't that mean that my actual member variable array is not updated? Do I need a temp reference or something? What's the right syntax for my situation here? Thanks!

Upvotes: 0

Views: 430

Answers (1)

Dinesh
Dinesh

Reputation: 3105

this is the way to do it: Your variable name is basically {$property} so when you do $this->$property[$key] I think PHP parser gets confused. I usually make sure that to explicitly state it to the parser that my variable name is $property which is done by using curly braces around variable.

Curly braces are used to explicitly specify the end of a variable name

class Sample {
    protected $foo = array();
    public function magicSampleSetFunc($property, $key, $value) {
        $this->{$property}[$key] = $value;
    }
}
...
$s = new Sample();
$s->magicSampleSetFunc('foo', 'someKey', 'someVal');

Upvotes: 2

Related Questions