What is stdClass in PHP?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified. But, if object type is converted/type-casted an instance of stdClass is created, if it is not NULL. If it is NULL, the new instance will be empty.

Uses:

  • The stdClass directly access the members by calling them.
  • It is useful in dynamic object.
  • It is used to set dynamic properties etc.

Program 1: Using array to storing data

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
      
// Array definition of an employee
$employee_detail_array = array(
    "name" => "John Doe",
    "position" => "Software Engineer",
    "address" => "53, nth street, city",
    "status" => "best"
);
  
// Display the array content
print_r($employee_detail_array);
?>

chevron_right


Output:

Array
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => best
)

Program 2: Using stdClass instead of array to store employee details (dynamic properties)



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
      
// Object-styled definition of an employee
$employee_object = new stdClass;
$employee_object->name = "John Doe";
$employee_object->position = "Software Engineer";
$employee_object->address = "53, nth street, city";
$employee_object->status = "Best";
      
// Display the employee contents
print_r($employee_object);
?>

chevron_right


Output:

stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)

Note: The type casting of array into object and object to array is possible.

Program 3: Converting array into object

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Aarray definition of an employee
$employee_detail_array = array(
    "name" => "John Doe",
    "position" => "Software Engineer",
    "address" => "53, nth street, city",
    "status" => "best"
);
  
// type casting from array to object
$employee = (object) $employee_detail_array;
      
print_r($employee);
?>

chevron_right


Output:

stdClass Object
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => best
)

Program 4: Converting object properties into array

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
      
// Object-styled definition of an employee
$employee_object = new stdClass;
$employee_object->name = "John Doe";
$employee_object->position = "Software Engineer";
$employee_object->address = "53, nth street, city";
$employee_object->status = "Best";
  
// The object is converted into array 
// using type casting
$employee_array = (array) $employee_object;
  
// Display the result in array
print_r($employee_array);
?>

chevron_right


Output:

Array
(
    [name] => John Doe
    [position] => Software Engineer
    [address] => 53, nth street, city
    [status] => Best
)


My Personal Notes arrow_drop_up

Fullstack developer and a security researcher experienced in Web Development Android and Windows application development Open Source Project Management Data Analytics and system development and a PASSIONATE TEACHER

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.