PHP Object Oriented Programming Built in Standard Class Common Data Type Converted to Object Type Example

  • 2021-12-12 03:53:48
  • OfStack

This article describes the PHP object-oriented programming built-in standard classes, common data types into object types. Share it for your reference, as follows:

Built-in standard class

In PHP, there are many "off-the-shelf classes", one of which is called "built-in standard class". This class "interior" can be considered as nothing.


class stdclass{ }


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

Run results:

object(stdClass)[1]

object(A)[2]

You can see that it is no different from ordinary classes.

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


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

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

Type conversion of objects

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

The grammatical form is:

$obj = (object) Other types of data;

Array to object: The key name of the array is regarded 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.


<?php
$config = array(
  'host' => "localhost",
  'port' => 3306,
  'user' => "root",
  'pass' => "123",
  'charset' => "utf8",
  'dbname' => "yeoman",
);
$obj1 = (object)$config;
var_dump($obj1);
echo "<br /> Take out separately user : " . $obj1->user;
?>

Run results:

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)

Take out user separately: root

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


<?php
$arr = array('pp1' => 1, 5 => 12);
$obj2 = (object)$arr;
var_dump($obj2);
echo "<br /> Take out separately pp1 : " . $obj2->pp1;
//echo "<br /> Take out separately 5 : " . $obj2->5;// Will report an error! 
?>

Run results:


$arr = array('pp1' => 1, 5 => 12);
$obj2 = (object)$arr;
var_dump($obj2);
echo "<br /> Take out separately pp1 : " . $obj2->pp1;
//echo "<br /> Take out separately 5 : " . $obj2->5;// Will report an error! 
?>

null Convert 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


<?php
$v1 = 1;
$v2 = 2.2;
$v3 = "abc";
$v4 = true;
$objv1 = (object)$v1;  // Integer to object type 
$objv2 = (object)$v2;  // Floating point type to object type 
$objv3 = (object)$v3;  // String type is an object type 
$objv4 = (object)$v4;  // Boolean to object type 
var_dump($objv1); echo "<br />";
var_dump($objv2); echo "<br />";
var_dump($objv3); echo "<br />";
var_dump($objv4); echo "<br />";

The running result is:

object(stdClass)[1]
public 'scalar' = > int 1

object(stdClass)[2]
public 'scalar' = > float 2.2

object(stdClass)[3]
public 'scalar' = > string 'abc' (length=3)

object(stdClass)[4]
public 'scalar' = > boolean true

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: