new stdClass()

基本介紹

  • 所屬語言:PHP
new stdClass()定義,英文解釋,例子,

new stdClass()定義

stdClass在PHP5才開始被流行。而stdClass也是zend的一個保留類。stdClass類是PHP的一個內部保留類,初始時沒有成員變數也沒成員方法,所有的魔術方法都被設定為NULL.凡是用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.

例子

<?phpfunction 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 = $value;            }        }        return $object;    }    return FALSE; }?>

相關詞條

熱門詞條

聯絡我們