How to define empty objects in php

小云云
Release: 2023-03-22 12:02:02
Original
4016 people have browsed it

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) {}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template