Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

stdClass

An empty class for objects with arbitrary properties.

Syntax

new stdClass()

Return value

stdClassAn object that accepts any properties.

Examples

An object without a class of its own:

<?php
 
$point = new stdClass();
$point->x = 3;
$point->y = 4;
 
echo json_encode($point), "\n";
 
$user = json_decode('{"name":"Anna"}');
echo get_class($user), ' ', $user->name;

Output:

{"x":3,"y":4}
stdClass Anna

Notes

json_decode() without the second argument and an (object) $array cast return stdClass objects. For your own data, classes with typed properties are better.

See also

In the tutorial

  • Classes and objects — What classes and objects are in PHP, how to describe properties and methods and create objects.

Thanks for reading!

Donate to the site — we work hard for you.