0

I get a value from a database, and if I dump it, I have:

myvar=stdClass::__set_state(array(
   'id' => '320646',
   'nameNormalized' => '27817759',
   'name' => 'Thename'
))

How could it be possible to create this same variable by hand? When I do this:

$myvar= new \stdClass(array(
   'id' => '320646',
   'nameNormalized' => '27817759',
   'name' => 'Thename',
));
var_export($myvar);

I get:

stdClass::__set_state(array(
))

Then how to do?

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • 1
    `stdClass` doesn't take a param. And there isn't an "array in it". You confuse the constructor with the `__set_state` method. What you're looking for is a typecast: `(object) array(...)`. – mario Feb 04 '15 at 06:29
  • @mario did beat me to it :D http://codepad.viper-7.com/6MOGYS – Havelock Feb 04 '15 at 06:31

1 Answers1

2

Here's the solution:

$myvar= (object)(array(
   'id' => '320646',
   'nameNormalized' => '27817759',
   'name' => 'Thename',
));
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • Hey, check this out http://blog.thoughtlabs.com/blog/2008/02/02/phps-mystical-__set_state-method – Vick Feb 04 '15 at 06:35