mercredi 1 avril 2015

PHP: reflexion, "non well formed numeric value encountered" setting array index

I haven't been able to find anything specific to this issue. In my class I need to take an associative array and put it's values in class variables. Any sub-arrays need to be converted to objects. The conversion to objects is happening in the following code:



foreach ($value as $key2 => $value2) {
$object = new $object_str($value2);
$this->$key[$object->getId()] = $object;
}


$value comes from an outer foreach loop.

$object_str contains the name of the object that has to be created,

like this: MartKuper\OnePageCRM\Contacts\ContactsUrl


The input array could look like this:



[
'url' => [
'type' => 'website',
'value' => 'google.com'
]
]


It should create a ContactsUrl object and add it to the $url class variable (which also is an array) based on the class' internal random id (uniqid()). Because I don't know how many 'url' entries the input array will have, this all needs to happen dynamically. Hence the



$this->$key[$object->getId()]


The error occurs on the index of the $key (url) array. It seems that it doesn't like to take a string as an index. I've tried putting hardcoded strings in



$this->$key['test]


that doesn't work either. When I put an integer in



$this->$key[1]


it does work. Converting the string to an integer is not an option. It will break a parser class that is used by many other classes.






Aucun commentaire:

Enregistrer un commentaire