PHP object-oriented - built-in standard classes and ordinary data types converted to object types

黄舟
Release: 2023-03-07 06:22:01
Original
1432 people have browsed it

Built-in standard classes

 PHPThere are many "ready-made classes", one of which is called the "built-in standard class". There is nothing "inside" this class.

class stdclass{  }
Copy after login
<?php$obj1 = new stdclass();
var_dump($obj1);class A{}$obj2 = new A();
var_dump($obj2);?>
Copy after login

Running result:

object(stdClass)[1]object(A)[2]
Copy after login

You can see that it is no different from the ordinary class.

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

$obj1->pp1 = 1;$obj2->port = &#39;3306&#39;;
Copy after login

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

Object type conversion

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

$obj = (object)其他类型数据;
Copy after login
  • Convert the array to an object: the key name of the array is used as the attribute name, and the value is the corresponding value of the object.

    • #Note: The attributes of digital subscripted data elements after being converted into objects cannot be obtained through object syntax, so conversion is not recommended.

<?php
$config = array(    &#39;host&#39; => "localhost",    &#39;port&#39; => 3306,    &#39;user&#39; => "root",    &#39;pass&#39; => "123",    &#39;charset&#39; => "utf8",    &#39;dbname&#39; => "yeoman",
);
$obj1 = (object)$config;
var_dump($obj1);
echo  "<br />单独取出user:" . $obj1->user;?>
Copy after login

Running result:

object(stdClass)[1]  public &#39;host&#39; => string &#39;localhost&#39; (length=9)  public &#39;port&#39; => int 3306
  public &#39;user&#39; => string &#39;root&#39; (length=4)  public &#39;pass&#39; => string &#39;123&#39; (length=3)  
  public &#39;charset&#39; => string &#39;utf8&#39; (length=4)  public &#39;dbname&#39; => string &#39;yeoman&#39; (length=6)


单独取出user:root
Copy after login

However, there are subscript elements in the array. If they are converted into objects, they cannot be obtained through object syntax.

<?php
$arr = array(&#39;pp1&#39; => 1, 5 => 12);
$obj2 = (object)$arr;
var_dump($obj2);
echo "<br />单独取出pp1:" . $obj2->pp1;//echo "<br />单独取出5:" . $obj2->5;//会报错!?>
Copy after login

Running result:

$arr = array(&#39;pp1&#39; => 1, 5 => 12);
$obj2 = (object)
$arr;
var_dump($obj2);
echo "<br />单独取出pp1:" . $obj2->pp1;//echo "<br />单独取出5:" . $obj2->5;//会报错!?>
Copy after login
  • nullConverted to object: empty object

$obj = (object)null;
Copy after login
  • Convert other scalar data to objects: the attribute name is fixed "scalar", and the value is the value of the variable

<?php$v1 = 1;       
$v2 = 2.2;
$v3 = "abc";
$v4 = true;
$objv1 = (object)
$v1;    //整型转为对象类型
$objv2 = (object)
$v2;    //浮点型转为对象类型
$objv3 = (object)
$v3;    //字符串型为对象类型
$objv4 = (object)
$v4;    //布尔转为对象类型
var_dump($objv1); echo "<br />";
var_dump($objv2); echo "<br />";
var_dump($objv3); echo "<br />";
var_dump($objv4); echo "<br />";
Copy after login

The running result is:

object(stdClass)[1]  public &#39;scalar&#39; => int 1
object(stdClass)[2]  public &#39;scalar&#39; => float 2.2
object(stdClass)[3]  public &#39;scalar&#39; => string &#39;abc&#39; (length=3)
object(stdClass)[4]  public &#39;scalar&#39; => boolean true
Copy after login

The above is the detailed content of PHP object-oriented - built-in standard classes and ordinary data types converted to object types. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!