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
php
<?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);
?>
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)
php
<?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);
?>
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
php
<?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);
?>
Output:
stdClass Object
(
[name] => John Doe
[position] => Software Engineer
[address] => 53, nth street, city
[status] => best
)
Program 4: Converting object properties into array
php
<?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);
?>
Output:
Array
(
[name] => John Doe
[position] => Software Engineer
[address] => 53, nth street, city
[status] => Best
)
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
What is the use of the @ symbol in PHP? The at sign (@) is used as error control operator in PHP. When an expression is prepended with the @ sign, error messages that might be generated by that expression will be ignored. If the track_errors feature is enabled, an error message generated by the expression and it will be saved in the varia
1 min read
Getting Started with PHP PHP (Hypertext Preprocessor) is a powerful scripting language widely used for web development. Whether you're looking to create dynamic web pages, handle form data, interact with databases, or build web applications, PHP has you covered. In this guide, we'll take you through the basics of PHP, cover
7 min read
PHP mb_strstr() Function The mb_strstr() function is an inbuilt function in PHP that finds the first occurrence of a given string in the main string, i.e. it will search for the occurrence of the first needle in haystack, & id found then the portion of the haystack will be returned, otherwise return false. Syntax: mb_st
2 min read
PHP | is_string() Function The is_string() function is an inbuilt function in PHP which is used to check whether the given value is a string or not. Syntax: bool is_string( mixed $var ) Parameters: This function accepts one parameter as mentioned above and described below: $var: It contains the value that need to check. Retur
1 min read
PHP mb_stristr() Function The mb_stristr() is an inbuilt function in PHP that is used to get the first occurrence of a string within another string. It will check case insensitivity. Syntax:mb_stristr( $haystack, $needle, $before_needle, $encoding = null ): string|falseParameters: This function accepts 4 parameters that are
2 min read