在 PHP 中执行 stdClass 时,我们可以这样做:
// Create new stdClass Object
$init = new stdClass;
// Add some test data
$init->foo = "Test data";
$init->bar = new stdClass;
$init->bar->baaz = "Testing";
$init->bar->fooz = new stdClass;
$init->bar->fooz->baz = "Testing again";
$init->foox = "Just test";
是否有任何其他替代方法可以使它看起来更干净,就像我们可以使用 JavaScript 一样?
谢谢。
最佳答案
$array = array('x'=>123);
$object = (object) $array;
关于PHP 写 stdClass 的另一种方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24404726/