๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ language.types.object.php
PHP: Objects - Manual
In PHP 7 there are a few ways to create an empty object: <?php $obj1 = new \stdClass; // Instantiate stdClass object $obj2 = new class{}; // Instantiate anonymous class $obj3 = (object)[]; // Cast empty array to object var_dump($obj1); // object(stdClass)#1 (0) {} var_dump($obj2); // object(class@anonymous)#2 (0) {} var_dump($obj3); // object(stdClass)#3 (0) {} ?> $obj1 and $obj3 are the same type, but $obj1 !== $obj3.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-objects
PHP Objects - GeeksforGeeks
April 17, 2025 - An object in PHP is an instance of a class. Objects are used to store data (properties) and define behaviors (methods) that can be used within an application.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_oop_classes_objects.asp
PHP OOP Classes and Objects
An object is an individual instance of a class. A class is defined with the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ php-objects
PHP Objects.
October 4, 2023 - In PHP, Object is a compound data type (along with arrays). Values of more than one types can be stored together in a single variable. Object is an instance of either a built-in or user defined class.
๐ŸŒ
Web Reference
webreference.com โ€บ php โ€บ basics โ€บ objects
Introduction to PHP objects
In PHP, objects are instances of a class, which is a blueprint for creating objects with specific properties and methods.
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php oop โ€บ php objects
PHP Objects and Classes - PHP Tutorial
April 7, 2025 - In this tutorial, you will learn about PHP objects, how to define a clas, and how to create an object from a class.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ php tutorial โ€บ php object type
PHP object type | Different PHP object types with Examples
April 5, 2023 - The following article provides an outline for PHP object type. An object is a datatype of Php which stores the data. It is an instance which is defined by a class. In order to create the object, we first need to define the class first, and then ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Zend
zend.com โ€บ blog โ€บ object-oriented-programming-php
PHP Object-Oriented Programming Basics | PHP OOP Guide | Zend
December 22, 2025 - Starting with PHP 5, the language offered full-fledged OOP support, allowing developers to create classes, methods, and properties that are essential for OOP. PHP's OOP features enable you to build scalable and secure web applications by encapsulating related logic into objects.
Find elsewhere
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ php tutorial โ€บ object in php
Object in PHP | Creating and Constructing Object | Methods | Properties
March 16, 2023 - Objects are real-world entities. Objects are defined from classes in Object-Oriented Programming like PHP. When a class is defined, we can create many objects out of the class. Example Class Car is defined, then Mercedes, BMW, Skoda are all ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-classes-and-objects
Php Classes and Objects - GeeksforGeeks
July 23, 2025 - The two fundamental building blocks in OOP are classes and objects. A class in PHP is a blueprint for creating objects. It defines the properties (variables) and methods (functions) that the objects created from the class will have.
Top answer
1 of 16
1013
$x = new stdClass();

A comment in the manual sums it up best:

stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces.

When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass whenever you need a generic object instance.

2 of 16
254

The standard way to create an "empty" object is:

$oVal = new stdClass();

But I personally prefer to use:

$oVal = (object)[];

It's shorter and I personally consider it clearer because stdClass could be misleading to novice programmers (i.e. "Hey, I want an object, not a class!"...).


(object)[] is equivalent to new stdClass().

See the PHP manual (here):

stdClass: Created by typecasting to object.

and here:

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.

and here (starting with PHP 7.3.0, var_export() exports an object casting an array with (object)):

Now exports stdClass objects as an array cast to an object ((object) array( ... )), rather than using the nonexistent method stdClass::__setState(). The practical effect is that now stdClass is exportable, and the resulting code will even work on earlier versions of PHP.


However remember that empty($oVal) returns false, as @PaulP said:

Objects with no properties are no longer considered empty.

Regarding your example, if you write:

$oVal = new stdClass();
$oVal->key1->var1 = "something"; // this creates a warning with PHP < 8
                                 // and a fatal error with PHP >=8
$oVal->key1->var2 = "something else";

PHP < 8 creates the following Warning, implicitly creating the property key1 (an object itself)

Warning: Creating default object from empty value

PHP >= 8 creates the following Error:

Fatal error: Uncaught Error: Undefined constant "key1"

In my opinion your best option is:

$oVal = (object)[
  'key1' => (object)[
    'var1' => "something",
    'var2' => "something else",
  ],
];
๐ŸŒ
CodeWithHarry
codewithharry.com โ€บ tutorial โ€บ php-objects
Objects | PHP Tutorial | CodeWithHarry
An Object is a self-contained component which consists of methods and properties to make a particular type of data useful. We can create multiple objects from a class and each object will have all the properties and methods defined in the class, but they will have different property values.
๐ŸŒ
PHP Guide
ifpb.github.io โ€บ php-guide โ€บ core โ€บ object
Object | PHP Guide - Github do IFPB
<?php class Post { public $title; public $text; public function __construct($title, $text) { $this->title = $title; $this->text = $text; } } $post = new Post('Lorem ipsum dolor', 'Nunc accumsan in ipsum a mattis...'); var_dump($post); //=> // object(Post)#1 (2) { // ["title"] => string(17) "Lorem ipsum dolor" // ["text"] => string(34) "Nunc accumsan in ipsum a mattis..." var_dump($post->title); //=> string(17) "Lorem ipsum dolor"
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ php-objects-and-references
PHP Objects and references
March 15, 2026 - The main difference between object assignment ($obj2 = $obj1) and object reference ($obj2 = &$obj1) is that both create variables pointing to the same object, but references create true aliases where the variables themselves are linked. PHP objects are always passed by reference through object identifiers.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_object_oriented.htm
Object Oriented Programming in PHP
Function definitions look much like standalone PHP functions but are local to the class and will be used to set and access object data.
๐ŸŒ
TommyDo
tommydo.ca โ€บ home โ€บ 3 ways create php objects
3 ways create PHP objects - TommyDo
December 17, 2023 - class Vehicles { public function park() { echo "Safe parked"; } } // Call Vehicles class $Vehicles = new Vehicles; $Vehicles->park(); // Check $Vehicles variable echo gettype($Vehicles); // Output: object ยท As usual usage, we always define an Array to create a block of information like this: $vehicle = [ 'id' => '1234', 'year' => '2022', 'make' => 'Subaru', 'model' => 'Crosstrek' ] ... $vehicle = new stdClass(); $vehicle->id = '1234'; $vehicle->year = '2022'; $vehicle->make = 'Subaru'; $vehicle->model = 'Crosstrek; // and call a item echo $vehicle->make; // Output: Subaru ยท The stdClass is the empty class in PHP which is used to cast other types to object.