This article mainly shares with you the method of defining empty objects in PHP. Sometimes we directly define the subscript value of an array that does not exist, and no error will be reported. However, when we define an object that does not exist, an error will be reported. This At this time, we can just define an empty object. There are three methods:
<?php$obj1 = new \stdClass; // Instantiate stdClass object$obj2 = new class{}; // Instantiate anonymous class$obj3 = (object)[]; // Cast empty array to objectvar_dump($obj1); // object(stdClass)#1 (0) {}var_dump($obj2); // object(class@anonymous)#2 (0) {}var_dump($obj3); // object(stdClass)#3 (0) {}
Related recommendations:
Can we use js to determine the json empty object
JavaScript Simple method to determine whether an object {} is an empty object
About json empty object filtering and replacement in jQuery_jquery
The above is the detailed content of How to define empty objects in php. For more information, please follow other related articles on the PHP Chinese website!