php创建没有类的对象 [英] php create object without class
本文介绍了php创建没有类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在php中创建匿名对象
Possible Duplicate:
Creating anonymous objects in php
在JavaScript中,您可以通过以下方式轻松创建不带类的对象:
In JavaScript, you can easiliy create an object without a class by:
myObj = {};
myObj.abc = "aaaa";
对于PHP,我已经找到了这个,但是已经有将近4年的历史了: http://www.subclosure.com/php-creating -anonymous-objects-on-the-fly.html
For PHP I've found this one, but it is nearly 4 years old: http://www.subclosure.com/php-creating-anonymous-objects-on-the-fly.html
$obj = (object) array('foo' => 'bar', 'property' => 'value');
现在在2013年使用PHP 5.4,还有其他替代方法吗?
Now with PHP 5.4 in 2013, is there an alternative to this?
推荐答案
,您始终可以使用new stdClass()
.示例代码:
you can always use new stdClass()
. Example code:
$object = new stdClass();
$object->property = 'Here we go';
var_dump($object);
/*
outputs:
object(stdClass)#2 (1) {
["property"]=>
string(10) "Here we go"
}
*/
从PHP 5.4开始,您还可以通过以下方式获得相同的输出:
Also as of PHP 5.4 you can get same output with:
$object = (object) ['property' => 'Here we go'];
这篇关于php创建没有类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文