This article analyzes the usage of stdClass in php with examples. Share it with everyone for your reference. The specific analysis is as follows:
stdclass is one of several predefined classes in php and is a class reserved by zent. In fact, it is a base class provided by PHP, which is a blank class with nothing in it. We can instantiate it, then define a series of variables, and pass variables through it (many PHP programmers use it to Pass the values of a series of variables while not bothering to create a class of your own). However, since methods cannot be added after instantiation, only properties can be passed. Because once a class is instantiated, methods cannot be added.
stdclass can be used as a base class. Its biggest feature is that (its derived classes) can automatically add member variables without stating them when defining.
All php variables are instances of stdClass.
How to use:
1. Use stdclass:
$andy = array(); $andy = (object)$andy; $andy->a = 1; $andy->b = 2; $andy->c = 3;
In this way, the quantities a, b, and c are filled in stdclass. This saves trouble, because to create an empty object requires $andy = new Andy; and there must be a class Andy{} first. Another example:
<?php $a = new stdClass(); $a->id = '11 '; $a->username = 'me'; print_r($a); ?>
will output: stdClass Object ( [id] => 11 [username] => me ).
Many times this method is used to replace the use of arrays, which is just a different syntax.
2. Read:
stdClass Object ( [getWeatherbyCityNameResult] => stdClass Object ( [string] => Array ( [0] => 四川 [1] => 成都 [2] => 56294 [3] => 56294.jpg [4] => 2009-5-17 13:52:08 [5] => 26℃/19℃ [6] => 5月17日 阴转阵雨 ) ) )
is actually similar to array, but the access method changes a little. We are generally used to using array['key'] to access arrays.
For this kind of stdClass, as in the above example, $weather->getWeatherbyCityNameResult->string[0] can access the attribute like this, and the result will be "Sichuan".
3. Instantiation, new.
Compare these two codes:
<?php $a = array(1=>2,2=>3); $a = (object)$a; $a->id = '11 '; $a->username = 'me'; print_r($a); ?>
will output: stdClass Object ( [1] => 2 [2] => 3 [id] => 11 [username] => me ) .
<?php $a = array(1=>2,2=>3); $a = (object)$a; $a = new stdClass(); $a->id = '11 '; $a->username = 'me'; print_r($a); ?>
will output: stdClass Object ( [id] => 11 [username] => me ).
It turns out that after instantiating with new, the previous array is cleared, leaving only the ones added later. If not instantiated, stdClass will retain all elements.
It should be noted that when using global or static in a function and new stdclass is referenced, &new stdclass will be invalid. You should avoid using references and use new stdclass directly.
I hope this article will be helpful to everyone’s PHP programming design.
For more articles related to the usage analysis of stdClass in php, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
