请输入您要查询的百科知识:

 

词条 new stdClass()
释义

new stdClass()定义

stdClass在PHP5才开始被流行。而stdClass也是zend的一个保留类。stdClass是PHP的一个基类,所有的类几乎都继承这个类,所以任何时候都可以被new,可以让这个变量成为一个object。同时,这个基类又有一个特殊的地方,就是没有方法。凡是用new stdClass()的变量,都不可能会出现$a->test()这种方式的使用。PHP5的对象的独特性,对象在任何地方被调用,都是引用地址型的,所以相对消耗的资源会少一点。在其它页面为它赋值时是直接修改,而不是引用一个拷贝。

英文解释

The PHP stdClass() is something that isn’t well documented but i will try to shed some light into the matter. stdClass is a default PHP object which has no predefined members. The name stdClass is used internally by Zend and is reserved. So that means that you cannot define a class named stdClass in your PHP code.

It can be used to manually instantiate generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.

例子

<?php

function arrayToObject($array) {

if(!is_array($array)) {

return $array; }

$object = new stdClass();

if (is_array($array) && count($array) > 0) {

foreach ($array as $name=>$value) {

$name = strtolower(trim($name));

if (!empty($name)) {

$object->$name = arrayToObject($value);

}

}

return $object;

}

else { return FALSE; }

}

?>

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/1/9 22:23:06