PHP 中的 stdClass 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/931407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is stdClass in PHP?
提问by Keira Nighly
Please define what stdClassis.
请定义是什么stdClass。
采纳答案by Alex Martelli
stdClassis PHP's generic empty class, kind of like Objectin Java or objectin Python (Edit:but not actually used as universal base class; thanks @Ciaran forpointing this out).
stdClass是 PHP 的通用空类,有点像ObjectJava 或objectPython(编辑:但实际上并未用作通用基类;感谢@Ciaran指出这一点)。
It is useful for anonymous objects, dynamic properties, etc.
它对匿名对象、动态属性等很有用。
An easy way to consider the StdClass is as an alternative to associative array. See this example below that shows how json_decode()allows to get an StdClass instance or an associative array.
Also but not shown in this example, SoapClient::__soapCallreturns an StdClass instance.
考虑 StdClass 的一种简单方法是作为关联数组的替代方案。请参阅下面的示例,该示例显示了如何json_decode()获取 StdClass 实例或关联数组。同样但未在此示例中显示,SoapClient::__soapCall返回一个 StdClass 实例。
<?php
//Example with StdClass
$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);
echo $stdInstance->foo . PHP_EOL; //"bar"
echo $stdInstance->number . PHP_EOL; //42
//Example with associative array
$array = json_decode($json, true);
echo $array['foo'] . PHP_EOL; //"bar"
echo $array['number'] . PHP_EOL; //42
See Dynamic Properties in PHP and StdClassfor more examples.
有关更多示例,请参阅PHP 和 StdClass 中的动态属性。
回答by Ciaran McNulty
stdClassis just a generic 'empty' class that's used when casting other types to objects. Despite what the other two answers say, stdClassis notthe base class for objects in PHP. This can be demonstrated fairly easily:
stdClass只是一个通用的“空”类,用于将其他类型转换为对象。不管其他两个答案怎么说,stdClass它不是PHP 中对象的基类。这可以很容易地证明:
class Foo{}
$foo = new Foo();
echo ($foo instanceof stdClass)?'Y':'N';
// outputs 'N'
I don't believe there's a concept of a base object in PHP
我不相信 PHP 中有基础对象的概念
回答by Mrinmoy Ghoshal
stdClass is a another great PHP feature. You can create a anonymous PHP class. Lets check an example.
stdClass 是另一个很棒的 PHP 特性。您可以创建一个匿名 PHP 类。让我们检查一个例子。
$page=new stdClass();
$page->name='Home';
$page->status=1;
now think you have a another class that will initialize with a page object and execute base on it.
现在想想你有另一个类,它将用一个页面对象初始化并在它的基础上执行。
<?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';
}
}
Now you have to create a new PageShow object with a Page Object.
现在您必须使用 Page 对象创建一个新的 PageShow 对象。
Here no need to write a new Class Template for this you can simply use stdClass to create a Class on the fly.
这里无需为此编写新的类模板,您只需使用 stdClass 即可动态创建类。
$pageview=new PageShow($page);
$pageview->show();
回答by mdpatrick
Also worth noting, an stdClassobject can be created from the use of json_decode()as well.
另外值得注意的是,一个stdClass对象也可以从使用中创建json_decode()。
回答by Bandula Dharmadasa
Using stdClassyou can create a new object with it's own properties. Consider the following example that represents the details of a user as an associative array.
使用stdClass您可以创建一个具有自己属性的新对象。考虑以下将用户详细信息表示为关联数组的示例。
$array_user = array();
$array_user["name"] = "smith john";
$array_user["username"] = "smith";
$array_user["id"] = "1002";
$array_user["email"] = "[email protected]";
If you need to represent the same details as the properties of an object, you can use stdClassas below.
如果您需要表示与对象属性相同的细节,您可以使用如下所示的stdClass。
$obj_user = new stdClass;
$obj_user->name = "smith john";
$obj_user->username = "smith";
$obj_user->id = "1002";
$obj_user->email = "[email protected]";
If you are a Joomla developer refer this example in the Joomla docsfor further understanding.
如果您是 Joomla 开发人员,请参阅Joomla 文档中的此示例以进一步了解。
回答by Codename-Steeve-Knight
Likewise,
同样地,
$myNewObj->setNewVar = 'newVal';
yields a stdClass object - auto casted
产生一个 stdClass 对象 - 自动转换
I found this out today by misspelling:
我今天通过拼写错误发现了这一点:
$GLOBASLS['myObj']->myPropertyObj->myProperty = 'myVal';
Cool!
凉爽的!
回答by John Smith
stdClass is not an anonymous class or anonymous object
stdClass 不是匿名类或匿名对象
Answers here includes expressions that stdClassis an anonymous class or even anonymous object. It's not a true.
这里的答案包括stdClass匿名类甚至匿名对象的表达式。这不是真的。
stdClassis just a regular predefined class. You can check this using instanceofoperator or function get_class. Nothing special goes here. PHP uses this class when casting other values to object.
stdClass只是一个常规的预定义类。您可以使用instanceofoperator 或 function进行检查get_class。这里没什么特别的。PHP 在将其他值转换为对象时使用此类。
In many cases where stdClassis used by the programmers the array is better option, because of useful functions and the fact that this usecase represents the data structure not a real object.
在stdClass程序员使用的许多情况下,数组是更好的选择,因为有用的函数以及这个用例代表数据结构而不是真实对象的事实。
回答by Antony
Its also worth noting that by using Casting you do not actually need to create an object as in the answer given by @Bandula. Instead you can simply cast your array to an object and the stdClass is returned. For example:
还值得注意的是,通过使用 Casting,您实际上并不需要像@Bandula 给出的答案那样创建对象。相反,您可以简单地将数组转换为对象,然后返回 stdClass。例如:
$array = array(
'Property1'=>'hello',
'Property2'=>'world',
'Property3'=>'again',
);
$obj = (object) $array;
echo $obj->Property3;
Output: again
Output: again
回答by Sage
Actually I tried creating empty stdClass and compared the speed to empty class.
实际上我尝试创建空的 stdClass 并将速度与空类进行比较。
class emp{}
then proceeded creating 1000 stdClasses and emps... empty classes were done in around 1100 microseconds while stdClasses took over 1700 microseconds. So I guess its better to create your own dummy class for storing data if you want to use objects for that so badly (arrays are a lot faster for both writing and reading).
然后继续创建 1000 个 stdClasses 和 emps……空类在大约 1100 微秒内完成,而 stdClasses 花费了 1700 微秒。因此,我想如果您非常想使用对象来存储数据,最好创建自己的虚拟类来存储数据(数组的写入和读取速度要快得多)。
回答by DevWL
stdClass objects in use
正在使用的 stdClass 对象
The stdClass allows you to create anonymous classes and with object casting you can also access keys of an associative array in OOP style. Just like you would access the regular object property.
stdClass 允许您创建匿名类,并且通过对象转换,您还可以以 OOP 样式访问关联数组的键。就像访问常规对象属性一样。
Example
例子
class Example {
private $options;
public function __construct(Array $setup)
{
// casting Array to stdClass object
$this->options = (object) $setup;
// access stdClass object in oop style - here transform data in OOP style using some custom method or something...
echo $this->options->{'name'}; // ->{'key'}
echo $this->options->surname; // ->key
}
}
$ob1 = new Example(["name" => "John", "surname" => "Doe"]);
will echo
会回声
John Doe
约翰·多伊

