Sparkler
0
Q:

how to create object in php

$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"
}
*/
1
   $object = new stdClass();
   $object->property = 'Here we go';

   var_dump($object);
   /*
   outputs:

   object(stdClass)#2 (1) {
      ["property"]=>
      string(10) "Here we go"
    }
   */
1
$x = new stdClass();
1
//object init
  $object = (object) [
    'propertyOne' => 'foo',
    'propertyTwo' => 42,
  ];
0
//define a class
class MyClass{
  //create properties, constructor or methods
}

//create object using "new" keyword
$object = new MyClass();
 
//or wihtout parenthesis
$object = new MyClass;
0

New to Communities?

Join the community