惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
量子位
腾讯CDC
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
B
Blog
S
SegmentFault 最新的问题
P
Privacy & Cybersecurity Law Blog
T
Threatpost
博客园 - 聂微东
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
C
Check Point Blog
N
Netflix TechBlog - Medium
D
DataBreaches.Net
爱范儿
爱范儿
IT之家
IT之家
S
Secure Thoughts
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
有赞技术团队
有赞技术团队
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
Schneier on Security
Schneier on Security
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
雷峰网
雷峰网
Project Zero
Project Zero
博客园 - Franky
H
Heimdal Security Blog
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - Tinywan

响应错误: Indirect modification of overloaded element of app\model\StudentCacheModel has no effect - Tinywan Ubuntu 22.04 编译安装 PHP 7.4.33 报错:make: *** [Makefile:749: ext/openssl/openssl.lo] Error 1 how to set mpdf HTML contains invalid UTF-8 character(s) 和 CPU 100% Redis Docekr WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition Nginx添加第三方模块,出现“is not binary compatible in”错误的解决方案 Docker 数据库连接见解异常 SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Try again 解决mysql死锁问题 SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction Java系列 | 如何讲自己的JAR包上传至阿里云maven私有仓库【云效制品仓库】 Redis系列 | 分类树查询功能如何从2s优化到0.1s Docker系列 | docker endpoint for “default” not found Node系列 | Node版本管理工具 fnm Java系列 | IntelliJ IDEA 如何导入和使用一个Jar包 Chrome扩展插件:Console Importer(控制台导入器) PHP系列 | mPdf字体库异常 Cannot find TTF TrueType font file "Eeyek.ttf" in configured font directories 网络安全 | 记录一次acme.sh更新证书 Error add txt for domain:_acme-challenge.tinywan.com 大数据系列 | 阿里云datav数据可视化(使用json文件生成可视化动态图标) Docker系列 | SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Try again768 Java系列 | Linux系统中运行JMeter脚本 PHP系列 | TP6使用表达式设置数据 Db::raw('end_time')
PHP系列 | PHP中的stdClass是什么?
Tinywan · 2022-06-02 · via 博客园 - Tinywan

简介

stdClass 是 PHP 中的空类,用于将其他类型转换为对象。它类似于 Java 或 Python 对象。 stdClass 不是对象的基类

转换为对象

如果将一个对象转换成对象,它将不会有任何变化。如果其它任何类型的值被转换成对象,将会创建一个内置类 stdClass 的实例。如果该值为 null,则新的实例为空。 array 转换成 object 将使键名成为属性名并具有相对应的值。注意:在这个例子里, 使用 PHP 7.2.0 之前的版本,数字键只能通过迭代访问。

stdClass() 的定义

1、stdClass是PHP的一个基类,几乎所有的类都继承这个类,任何时候都可以被new,可以让一个变量成为一个对象(object)。

2、所有使用 new stdClass 的变量,都不能使用方法,即不可能出现 $a->text() 的情况 

3、stdClass 在 php5 版本开始流行起来的,低于php5的版本,尽量或不使用此方法(好像这一条是多余的)

stdClass() 的用途

1、stdClass通过调用它们直接访问成员。

2、它在动态对象中很有用。

3、它用于设置动态属性等。

stdClass 类的使用

1、存储数据

(1)数组存储

1 // 定义数组存储个人信息
2 $personal_array = [
3     "name" => "Tinywan",
4     "home" => "www.tinywan.com",
5     "address" => "ZheJiang HangZhou"
6 ];
7 
8 // 显示数组内容
9 print_r($personal_array);

输出内容

Array
(
    [name] => Tinywan
    [home] => www.tinywan.com     
    [address] => ZheJiang HangZhou
)

(2)stdClass存储:使用 stdClass 而不是数组来存储个人详细信息(动态属性)

$personal_object = new stdClass();
$personal_object->name = "Tinywan";
$personal_object->home = "www.tinywan.com";
$personal_object->address = "ZheJiang HangZhou";

// 显示对象内容
print_r($personal_object);

输出内容

stdClass Object
(
    [name] => Tinywan
    [home] => www.tinywan.com     
    [address] => ZheJiang HangZhou
)

注意:数组到对象和对象到数组的类型转换是可能的。

(3)将数组转换为对象

$personal_array = [
    "name" => "Tinywan",
    "home" => "www.tinywan.com",
    "address" => "ZheJiang HangZhou"
];
$personal_object = (object) $personal_array;

// 显示对象内容
print_r($personal_object);

输出内容

stdClass Object
(
    [name] => Tinywan
    [home] => www.tinywan.com     
    [address] => ZheJiang HangZhou
)

(4)将对象属性转换为数组

$personal_object = new stdClass();
$personal_object->name = "Tinywan";
$personal_object->home = "www.tinywan.com";
$personal_object->address = "ZheJiang HangZhou";

$personal_array = (array) $personal_object;
// 显示数组内容
print_r($personal_array);

输出内容

Array
(
    [name] => Tinywan
    [home] => www.tinywan.com
    [address] => ZheJiang HangZhou
)

2、动态增加属性

$personal_object = new stdClass();
$personal_object->name = "Tinywan";
$personal_object->home = "www.tinywan.com";
$personal_object->address = "ZheJiang HangZhou";
print_r($personal_object);

// 动态增加属性
$personal_object->age = 24;
$personal_object->schoole = "GanZhengFa";
print_r($personal_object);

输出

stdClass Object
(
    [name] => Tinywan
    [home] => www.tinywan.com
    [address] => ZheJiang HangZhou
)
stdClass Object
(
    [name] => Tinywan
    [home] => www.tinywan.com
    [address] => ZheJiang HangZhou
    [age] => 24
    [schoole] => GanZhengFa
)

 stdClass 并不是 PHP 中对象的基类

这里需要注意的是,尽管是泛型类,stdClass 并不是 PHP 中对象的基类,我们可以使用 instanceof 关键字来证明这一点。

class Tinywan{

}

$objClass = new Tinywan();
if ($objClass instanceof \stdClass){
    echo 'Yes';
} else {
    echo 'No';
}

输出结果:No

这阐明了 stdClass 不是 PHP 中对象的基类

 使用 json_encode() 和 json_decode()

json_encode() 和 json_decode() 是专门用于对 JSON 字符串执行操作的函数。json_encode() 用于将 Array 转换为 JSON字符串 。因此,首先,我们将一个对象转换为 JSON 字符串,然后使用 json_decode() 将其转换为对象。

$empInfo = array(
'name'=>'John',
'address'=>'Houston',
'employment' => array(
    'id' => '1',
    'address' => 'Los Angeles'
    )
);
print_r(json_decode(json_encode($empInfo)));

输出结果

stdClass Object
(
   [name] => John
   [address] => Houston
   [employment] => stdClass Object
      (
         [id] => 1
         [address] => Los Angeles
      )
)