Developer Snippet Diary

What is stdClass in PHP?

  • stdClass (standard class) is PHP's empty object. Think of it like a blank box where you can create properties on the fly without making a full class.
  • It's useful when you quickly need an object without defining a full class {}.
// Create a new stdClass object
$user = new \stdClass();

// Add properties
$user->id = 1;
$user->name = "Ali Khan";
$user->email = "ali@example.com";

// Access properties
echo $user->name; // Ali Khan

// Show full object
print_r($user);
stdClass Object
(
    [id] => 1
    [name] => Ali Khan
    [email] => ali@example.com
)

Prepend object to existing object:

$user->prepend($aNewObj);

Append at last: 

$user->push($aNewObject);
Posted by: R GONDAL
Email: rizikmw@gmail.com