请定义什么是stdClass。


当前回答

如果您想快速创建一个新对象来保存关于一本书的一些数据。你会这样做:

$book = new stdClass;
$book->title = "Harry Potter and the Prisoner of Azkaban";
$book->author = "J. K. Rowling";
$book->publisher = "Arthur A. Levine Books";
$book->amazon_link = "http://www.amazon.com/dp/0439136369/";

详情请查看网站http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/。

其他回答

stdClass是另一个很棒的PHP特性。 您可以创建一个匿名PHP类。 让我们来看一个例子。

$page=new stdClass();
$page->name='Home';
$page->status=1;

现在假设您有另一个类,它将初始化一个页面对象并基于它执行。

<?php
class PageShow {

    public $currentpage;

    public function __construct($pageobj)
    {
        $this->currentpage = $pageobj;
    }

    public function show()
    {
        echo $this->currentpage->name;
        $state = ($this->currentpage->status == 1) ? 'Active' : 'Inactive';
        echo 'This is ' . $state . ' page';
    }
}

现在您必须使用Page object创建一个新的PageShow对象。

这里不需要写一个新的类模板,你可以简单地使用stdClass创建一个动态的类。

    $pageview=new PageShow($page);
    $pageview->show();

你也可以使用object将数组强制转换为你选择的对象:

Class Example
{
   public $name;
   public $age;
}

现在要创建一个Example类型的对象并初始化它,你可以做以下任何一件事:

$example = new Example();
$example->name = "some name";
$example->age = 22;

OR

$example = new Example();
$example = (object) ['name' => "some name", 'age' => 22];

第二种方法主要用于初始化具有许多属性的对象。

我们使用stdClass的原因是在PHP中没有办法区分普通数组和关联数组(就像在Javascript中,你用{}表示对象,用[]表示数组来区分它们)。

这就给空对象带来了一个问题。以这个为例。

PHP:

$a = [1, 2, 3]; // this is an array
$b = ['one' => 1, 'two' => 2]; // this is an associate array (aka hash)
$c = ['a' => $a, 'b' => $b]; // this is also an associate array (aka hash)

让我们假设您想要对变量$c进行JSON编码

echo json_encode($c);
// outputs => {'a': [1,2,3], 'b': {one: 1, two: 2}}

现在假设你删除了b美元的所有键,使它为空。因为$b现在是空的(你删除了所有的键,记得吗?),它看起来像[],如果你看它,它可以是一个数组或对象。

因此,如果再次执行json_encode,输出将有所不同

echo json_encode($c);
// outputs => {'a': [1,2,3], 'b': []}

这是一个问题,因为我们知道b应该是一个关联数组,但PHP(或任何像json_encode这样的函数)不是。

所以stdClass来救场了。再举一个同样的例子

$a = [1, 2, 3]; // this is an array
$b = (object) ['one' => 1, 'two' => 2]; // this makes it an stdClass
$c = ['a' => $a, 'b' => $b]; // this is also an associate array (aka hash)

所以现在即使你删除$b中的所有键并使其为空,因为它是一个stdClass,这并不重要,当你json_encode它时,你会得到这样的结果:

echo json_encode($c);
// outputs => {'a': [1,2,3], 'b': {}}

这也是为什么json_encode和json_decode默认返回stdClass的原因。

 $c = json_decode('{"a": [1,2,3], "b": {}}', true); //true to deocde as array
 // $c is now ['a' => [1,2,3], 'b' => []] in PHP
 // if you json_encode($c) again your data is now corrupted

php.net手册有一些可靠的解释和例子,用户贡献的stdClass是什么,我特别喜欢这个http://php.net/manual/en/language.oop5.basic.php#92123, https://stackoverflow.com/a/1434375/2352773。

stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces. When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass whenever you need a generic object instance. stdClass is NOT a base class! PHP classes do not automatically inherit from any class. All classes are standalone, unless they explicitly extend another class. PHP differs from many object-oriented languages in this respect. You could define a class that extends stdClass, but you would get no benefit, as stdClass does nothing.

stdClass在PHP中是经典的泛型类。它没有内置的属性或方法。基本上,它用于类型转换,创建具有动态属性的对象等。如果您有javascript背景,您可以确定为

$o = new \stdClass();

等于

const o = {};

它创建空对象,稍后由程序控制流填充。