In this lesson we will discuss
stdClass instances very briefly. We will get back to them and dig deeper some later.
stdClass objects are something like associative arrays. To create an instance of
stdClass you should use
new operator or cast an associative array to
$object1 = new stdClass; $object2 = (object) ;
You can access or add properties of (to)
stdClass with the arrow syntax:
$person = new stdClass; $person->name = 'Sam'; $person->age = 19; var_dump($person); $car = (object) [ 'mark' => 'Toyota', 'model' => 'Land Cruiser' ]; echo 'The mark of the car is ' . $car->mark;
You can access
stdClass property indirectly with a name stored in a variable:
$person = new stdClass; $person->name = 'Sam'; $person->age = 19; $field = 'name'; echo 'Field ' . $field . ' has value ' . $person->$field; # Prints "Field name has value Sam"
There are some differences between
stdClass instances and associative arrays though. We will discuss them some later. We will meet
stdClass in further lessons. However this lesson has given you all you need to know about
stdClass to go through those further lessons.