1$x = (object) [
2 'a' => 'test',
3 'b' => 'test2',
4 'c' => 'test3'
5];
6var_dump($x);
7
8/*
9object(stdClass)#1 (3) {
10 ["a"]=>
11 string(4) "test"
12 ["b"]=>
13 string(5) "test2"
14 ["c"]=>
15 string(5) "test3"
16}
17*/
1 $object = new stdClass();
2 $object->property = 'Here we go';
3
4 var_dump($object);
5 /*
6 outputs:
7
8 object(stdClass)#2 (1) {
9 ["property"]=>
10 string(10) "Here we go"
11 }
12 */
1//object init
2 $object = (object) [
3 'propertyOne' => 'foo',
4 'propertyTwo' => 42,
5 ];
1
2By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose:
3
4
5
6<?php $genericObject = new stdClass(); ?>
7
8
9
10I had the most difficult time finding this, hopefully it will help someone else!
11
1//define a class
2class MyClass{
3 //create properties, constructor or methods
4}
5
6//create object using "new" keyword
7$object = new MyClass();
8
9//or wihtout parenthesis
10$object = new MyClass;