Home > Backend Development > PHP Tutorial > stdClass_PHP tutorial in PHP

stdClass_PHP tutorial in PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:56:12
Original
967 people have browsed it

stdClass in PHP

If all Classes should exist in a default level, then the top level should be the most general Class, which is to say extremely abstract , each lower-level Class is more specialized than the upper-level Class (that is, the parent class). Based on this idea, in PHP, this top-level Class is named "stdClass", and as a "Standard Class", you can think of it as a class without any attributes and methods.

The purpose of using stdClass: Add properties to the base object (Base Object) at runtime

Question

You want to create an object and add some properties to it, but you don't want to formally define it as an explicit class. This is quite useful in some cases, for example when you define a method to return an object with certain attributes (undefined), such as when returning a result set object through mysql_fetch_object(), for certain attributes When you are not sure whether it exists and don’t want to check isset one by one.

Solution

Use PHP built-in class, stdClass, for example:

<?php
$sql="SELECT name,email FROM users WHERE id=$id";
$dbh=mysql_query($sql);
$obj=mysql_fetch_object($dbh);
if(!$obj) $obj=new StdClass;
$array=array(
	'name'=>$obj->name,
	'email'=>$obj->email
);
Copy after login

Finally, this usage of setting properties for an empty object at runtime is not difficult to see and will also bring us some problems, such as you cannot really determine who this object is and what its purpose is.

Articles you may be interested in

  • Usage of several keywords such as $this, static, final, const, self and so on in php
  • PHP Detailed explanation of string escape functions (addslashes, stripslashes)
  • Detailed explanation of the differences between empty, is_null, isset in PHP
  • How to delete elements in PHP arrays (unset, array_splice)?
  • php searches whether a value exists in an array (in_array(), array_search(), array_key_exists())
  • php string replacement function str_replace is faster than preg_replace
  • The difference between adding the static keyword before variables and functions in php
  • Analysis of issues regarding abstract classes and abstract methods in php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/989195.htmlTechArticlestdClass in PHP If all Classes should exist in a default hierarchy, then the top level should be The most general Class, that is to say extremely abstract, every lower level...
Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template