Statement on glibc/iconv Vulnerability

stdClass 类

(PHP 4, PHP 5, PHP 7, PHP 8)

简介

具有动态属性的通用空类。

此类的对象可以使用 new 运算符实例化,也可以通过类型转换为对象创建。几个 PHP 函数也会创建此类的实例,比如 json_decode()mysqli_fetch_object()PDOStatement::fetchObject()

尽管没有实现 __get()/__set() 魔术方法,但此类允许动态属性且不需要 #[\AllowDynamicProperties] 属性。

这不是 PHP 的基类,因为 PHP 没有通用基类的概念。然而,可以创建继承 stdClass 的自定义类,从而继承动态属性的功能。

类摘要

class stdClass {
}

此类没有方法和默认属性。

示例

示例 #1 通过类型转换为对象创建

<?php
$obj
= (object) array('foo' => 'bar');
var_dump($obj);

以上示例会输出:

object(stdClass)#1 (1) {
  ["foo"]=>
  string(3) "bar"
}

示例 #2 通过 json_decode() 创建

<?php
$json
= '{"foo":"bar"}';
var_dump(json_decode($json));

以上示例会输出:

object(stdClass)#1 (1) {
  ["foo"]=>
  string(3) "bar"
}

示例 #3 声明动态属性

<?php
$obj
= new stdClass();
$obj->foo = 42;
$obj->{1} = 42;
var_dump($obj);

以上示例会输出:

object(stdClass)#1 (2) {
  ["foo"]=>
  int(42)
  ["1"]=>
  int(42)
}
add a note

User Contributed Notes 1 note

up
7
Mats M
1 year ago
In PHP8 this has been changed

https://www.php.net/manual/en/migration80.incompatible.php

A number of warnings have been converted into Error exceptions:

Attempting to write to a property of a non-object. Previously this implicitly created an stdClass object for null, false and empty strings.

So if you add properties to a $var, you first need to make it a stdClass()

$var = new stdClass();
$var->propp1 = "nice";
$var->propp2 = 1234;
To Top