Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 19:49:07 09月22日 Update

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

Tags: Object type standard data result run attribute array article grammar example program programming analysis design subscript element content report store Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Redmi Xiaomi Linux