Creating a new instance of stdClass in PHP 7

See the PHP manual (here):
and here:
and here (starting with PHP 7.3.0,
exports an object casting an array with
):
However remember that
empty($oVal) returns false
, as @PaulP said:
Regarding your example, if you write:
PHP < 8 creates the following Warning, implicitly creating the property (an object itself) PHP >= 8 creates the following Error:
In my opinion your best option is:
Solution 3:
I want to point out that in PHP there is no such thing like empty object in sense:
but of course $obj will be empty.
Program 3:
Converting array into object
Output:
Program 4:
Converting object properties into array
Output:
PHP is a server-side scripting language designed specifically for web development.

PHP has a special class called stdClass that serves as a blank class. It can be used to transform other data types into objects, much like Java or Python. However, it is not the fundamental class for objects. When an item is transformed into an object, it does not change. However, if the item is cast as an object, a new stdClass instance is created, as long as it is not null. If it is null, the newly created instance will be empty.


Uses:

  • The members of stdClass can be accessed by a direct function call.
  • It is useful in dynamic object.
  • It is employed for configuring dynamic attributes and so on.

Program 1 involves the utilization of arrays for data storage.

The following codes will be used:


<?php






// Array definition of an employee

The next group of codes are:


$employee_detail_array



=



array



(

The remaining codes are as follows:





"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
)

Employing stdClass for storing employee information, as opposed to utilizing an array, with dynamic properties.

The following codes are listed:

<?php




// Object-styled definition of an employee


$employee_object

to

stdClass;


$employee_object

to

;


$employee_object

to

;


$employee_object

to

;


$employee_object

to

;




// Display the employee contents


print_r(

to

);


?>

Output:

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

It is possible to cast arrays into objects and vice versa.

Program 3 involves the transformation of an array into an object.

The following codes are included:

<?php




// Aarray definition of an employee


$employee_detail_array

to

(



to

,



to

,



to

,



to

"best"


);




// type casting from array to object


$employee

to

;




print_r(

to

);


?>

Output:

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

Fourth program:
Transforming properties of an object into an array.

The following codes are listed below:

<?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
)

This tutorial and examples are intended to teach you PHP, a server-side scripting language that is primarily used for web development. By starting from the basics, you can gradually gain a comprehensive understanding of PHP.

Frequently Asked Questions

Posted in Php