Skip to main content

How to Create a Blank Object in PHP

In PHP, a blank object is an object that doesn’t have any properties or methods. Creating a blank object is useful when you want to create a new object without modifying an existing one. You can also use a blank object to store data that you don’t need to access or modify later. In this snippet, you’ll learn how to create blank objects in PHP.

A blank object can be created using $obj = new stdClass();.

$invoice = new stdClass();
$invoice->id = 5;
$invoice->number = "INV-2022050005";
print_r($invoice);
Output:

stdClass Object
(
    [id] => 5
    [number] => INV-2022050005
)
//another method is to cast an array to an object
$arr = [];
$obj = (object) $arr;

By continuing to use the site, you agree to the use of cookies.