Create an anonymous stdClass object for test cases - possible?

Hello,

I’m having this array to test:

$response = array(
    'success' => TRUE,
    'data'       => array("1", "2", "3")
);

however, I would like to use an object so that I can simulate a stdClass object and figure it out how does it work on this case.

So, I was wondering if we can instantly create an object, the same way we do as the array?

Is this possible?

Regards,
Márcio

Just for completeness:

$object = new stdClass();
$object->foo = 'bar';

also works :wink:

Easiest way to emulate JavaScript’s “property list” style of declaring an anonymous object in PHP. :thumbup:

I’m not entirely sure what you mean, but maybe this?


<?php
$response = (object) array(
  'success' => true,
  'data'    => array(1, 2, 3)
);

var_dump(
  $response
);

/*
  object(stdClass)#1 (2) {
    ["success"]=>
    bool(true)
    ["data"]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
    }
  }
*/
?>

Perfect. It was exactly that! :slight_smile:
I was aware of (int) and others but not (object) and probably others…

Thanks a lot,
Márcio