PHP.nl

The stdClass class

The stdClass class

A generic empty class with dynamic properties.

Objects of this class can be instantiated with
 operator or created by
.
Several PHP functions also create instances of this class, e.g.
, 
or .

newtypecasting to objectjson_decode``mysqli_fetch_object``PDOStatement::fetchObject

Despite not implementing
/
magic methods, this class allows dynamic properties and does not require the
 attribute.

__get()__set()#[\AllowDynamicProperties]

This is not a base class as PHP does not have a concept of a universal base
class. However, it is possible to create a custom class that extends from
 and as a result inherits the functionality
of dynamic properties.

stdClass

stdClassThis class has no methods or default properties.

Voorbeeld: Created as a result of typecasting to object

<?php
$obj = (object) array('foo' => 'bar');
var_dump($obj);
object(stdClass)#1 (1) {
  ["foo"]=>
  string(3) "bar"
}

**Voorbeeld: Created as a result of **

<?php
$json = '{"foo":"bar"}';
var_dump(json_decode($json));
object(stdClass)#1 (1) {
  ["foo"]=>
  string(3) "bar"
}

Voorbeeld: Declaring dynamic properties

<?php
$obj = new stdClass();
$obj->foo = 42;
$obj->{1} = 42;
var_dump($obj);
object(stdClass)#1 (2) {
  ["foo"]=>
  int(42)
  ["1"]=>
  int(42)
}

Documentatie