php define object code example

Example 1: php create object

$x = (object) [
    'a' => 'test',
    'b' => 'test2',
    'c' => 'test3'
];
var_dump($x);

/*
object(stdClass)#1 (3) {
  ["a"]=>
  string(4) "test"
  ["b"]=>
  string(5) "test2"
  ["c"]=>
  string(5) "test3"
}
*/

Example 2: make a object php

$object = new stdClass();
   $object->property = 'Here we go';

   var_dump($object);
   /*
   outputs:

   object(stdClass)#2 (1) {
      ["property"]=>
      string(10) "Here we go"
    }
   */

Example 3: object php

//object init
  $object = (object) [
    'propertyOne' => 'foo',
    'propertyTwo' => 42,
  ];

Example 4: php define object

$x = new stdClass();

Example 5: php object

$o= new \stdClass();
$o->a = 'new object';

OR

$o = (object) ['a' => 'new object'];

Tags:

Misc Example