Dynamically declaring a variable inside a class in php

I'm having problems dynamically declaring variables inside a class / object in PHP.

I have a class called Column and it gets an associative array (names => values) of unknown length.

I need class variables in order to have the same name as in the array (and their values ​​must match).

Since the Variable Variables method is a bit confusing when you add the class / object declaration syntax on top of it, I feel like I lost a bit in it.

I tried something like this:

function __construct($array) { foreach ($array as $key => $value) { $this->$key; $this->key=$value; } } 

I usually didn’t do this

 $this->$key; 

I thought $this->key would match the syntax, but apparently I was wrong. If someone can help me fix it, it will be very helpful.

+5
source share
2 answers

Just use the syntax {}:

 function __construct($array) { foreach ($array as $key => $value) { $this->{$key} = $value; } } 
+5
source

Instead

 $this->$key; $this->key=$value; 

It should be

 $this->$key=$value; 

Who cares?

Variable property $this->$key; as a separate instruction, it accesses a property with a name in $key , but does nothing with it. But this will lead to the declaration of the Undefined property if the property has not been previously declared or dynamically assigned.

$this->key refers to the literal property of key , that is, public $key; if it was declared explicitly.

$this->$key refers to a property with the name stored in $key . Therefore, if $key === 'foo' is the same as $this->foo .

Assignment of dynamic properties that were not previously declared does not depend on the use of variable properties. Therefore, if the $foo property does not exist, $this->foo = 1 matches $this->$key = 1 with $key === 'foo' . Both create a public $foo property for the current object.

Special occasions

If $key not a string, it will be passed to the string. If it is an object or resource that cannot be passed to a string, you will receive an inability to convert the string to a string.

If $key converted to an empty string, you will receive a Can not access empty property error.

If $key starts with a number or contains special characters that are usually not allowed in variable names (for example, $ ), a property will be created anyway. Access to properties with such names is always possible if you use {} to use arbitrary expressions as property names. Try the following:

 $o = new stdclass; $o->{'!"Β§$%'} = 'it works'; echo $o->{'!"Β§$%'}; 
0
source

All Articles