Table of Contents
Usage analysis of stdClass in php
Home Backend Development PHP Tutorial Usage analysis of stdClass in php_PHP tutorial

Usage analysis of stdClass in php_PHP tutorial

Jul 13, 2016 am 10:05 AM
php main analyze article usage of

Usage analysis of stdClass in php

This article mainly introduces the usage of stdClass in php, and analyzes the functions and usage techniques of stdClass with examples. It has certain reference value and is needed Friends can refer to it

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 the need to specify them when defining.

All php variables are instances of stdClass.

How to use:

1. Use stdclass:

?

1

2

3

4

5

$andy = array();

$andy = (object)$andy;

$andy->a = 1;

$andy->b = 2;

$andy->c = 3;

1 2

3

4

5

1

2

3

4

5

6

$a = new stdClass();

$a->id = '11 ';

$a->username = 'me';

print_r($a);

?>

$andy = array(); $andy = (object)$andy;

$andy->a = 1;

$andy->b = 2;

$andy->c = 3;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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日 阴转阵雨

)

)

)

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: ?
1 2 3 4 5 6 <🎜>$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: ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 stdClass Object ( [getWeatherbyCityNameResult] => stdClass Object ( [string] => Array ( [0] => Sichuan [1] => Chengdu [2] => 56294 [3] => 56294.jpg [4] => 2009-5-17 13:52:08 [5] => 26℃/19℃ [6] => May 17, cloudy to showers ) ) )

In fact, it is similar to array, but the access method changes a little. We are generally accustomed 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:

?

1

2

3

4

5

6

7

$a = array(1=>2,2=>3);

$a = (object)$a;

$a->id = '11 ';

$a->username = 'me';

print_r($a);

?>

1 2

3

4

5

6

1

2

3

4

5

6

7

8

$a = array(1=>2,2=>3);

$a = (object)$a;

$a = new stdClass();

$a->id = '11 ';

$a->username = 'me';

print_r($a);

?>

7

$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 ). ?
1 2
3 4 5 6 7 8
<🎜>$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. http://www.bkjia.com/PHPjc/961777.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/961777.htmlTechArticleUsage analysis of stdClass in php. This article mainly introduces the usage of stdClass in php and analyzes the functions of stdClass with examples. and usage skills, which have certain reference value. Friends in need can refer to...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles