Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of built-in Standard Class in PHP object-oriented programming

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the PHP object-oriented programming built-in standard class example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

The details are as follows:

Built-in standard class

In PHP, there are many "off-the-shelf classes", one of which is called "built-in standard class". The "inside" of this class can be thought of as nothing.

Class stdclass {}

Running result:

Object (stdClass) [1]

Object (A) [2]

As you can see, it is no different from the ordinary class.

The purpose of the built-in standard class is to store some temporary simple data, such as:

$obj1- > pp1 = 1bot obj2-> port = '3306'

It can also be used to store data during type conversion.

Type conversion of object

Other data types are converted to object types, and the result is an object of the built-in standard class (stdclass).

The grammatical form is:

$obj = (object) other types of data

The array is converted to an object: the key name of the array is treated as the attribute name, and the value is the corresponding value of the object.

Note: numeric subscript data elements, converted to object attributes, cannot be obtained through object syntax, so conversion is not recommended.

Running result:

Object (stdClass) [1]

Public 'host' = > string' localhost' (length=9)

Public 'port' = > int 3306

Public 'user' = > string' root' (length=4)

Public 'pass' = > string' 123' (length=3)

Public 'charset' = > string' utf8' (length=4)

Public 'dbname' = > string' yeoman' (length=6)

Separate removal of user:root

However, there are subscript elements in the array, which cannot be obtained through object syntax if converted to an object.

Running result:

$arr = array ('pp1' = > 1, 5 = > 12); $obj2 = (object) $arr;var_dump ($obj2); echo "take out pp1:" alone. $obj2- > pp1;//echo "take out 5:" separately. $obj2- > 5 position / will report an error! ? >

Convert null to object: empty object

$obj = (object) null

Other scalar data is converted to an object: the property name is fixed "scalar" and the value is the value of the variable

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report