phparraysobjectstdclass

set object property names


Maybe is a dummy question but I'd like to know the right way to achieve this:

i have an array of strings; I 'd like to use it as properties for my stdclass object; Why I cannot prepare it and then use it without give it a value on fly?

$heading= ["cgonboardingid","username","firstname","lastname","departement","lang","cgrole"];
$tmpUser =   new \stdClass();

this code won't work:

 foreach ($headings as $head){
                            $tmpUser->$head;
                        }

neither if i try to use {$head} or {'$head'} This works!!!

 foreach ($headings as $head){
                            $tmpUser->$head='something';
                        }

Solution

  • you can use null, false or '' instead of 'something'