什麼是PHP中的stdclass()

什麼是PHP中的stdclass()
php

看到有人用這個,不知道爲什麼,找到以下文章解釋:
mysql

中文的,應用自http://www.stud.uni-karlsruhe.de/~uu5i/blog/index.php?aid=261
程序員


php的stdClass是什麼
sql

簡述
ide

這兩天看drupal的代碼,發現他經常使用這個類
fetch

但是查了整個文件也沒找到stdClass的定義。估計是內置對象,查手冊。手冊上查到了,stdClass是zent保留的一個類。僅此而已?



google中文一查,可能是php手冊上的統一句話。後來看到maboo中國上有人提這個問題,哪一個lang3居然回答讓別人善用資源管理器??太不厚道了。



google查所有語言。總算查到一點信息。

http://forum.mamboserver.com/showthread.php?t=936

原來就是基類。不少php程序員用它來傳遞一系列變量的值,而同時又懶得去建立一個本身的類。

這個基類只能傳遞屬性,而不能定義方法。由於,一旦類被實列化之後,就不能在添加方法了。

再說的明白一點,這個stdClass就相似於C++裏面的structur。你能夠用它來存儲不少變量屬性,可是沒有方法。就是這樣。

stdClass is a default PHP object.

ui


這篇文章還引用到另外一篇英文的文章,對stdclass作了詳細解釋:
this


This might shed some light on the subject: From PHP CookBook



Classes also live in a defined hierarchy. At the top of the chain, there is a generic class. In

PHP, this class is named stdClass, for "standard class." Each class down the line is more

specialized than its parent.



Recipe 7.12 Adding Properties to a Base Object

7.12.1 Problem

You want to create an object and add properties to it, but you don't want to formally define it

as a specific class. This is useful when you have a function that requires an object with certain

properties, such as what's returned from mysql_fetch_object( ) or imap_header( ).

7.12.2 Solution

Use the built-in base class, stdClass:

$pickle = new stdClass;

$pickle->type = 'fullsour';



7.12.3 Discussion

Just as array( ) returns an empty array, creating an object of the type stdClass provides

you with an object without properties or methods.

Like objects belonging to other classes, you can create new object properties, assign them

values, and check those properties:

$guss = new stdClass;

$guss->location = 'Essex';

print "$guss->location/n";

$guss->location = 'Orchard';

print "$guss->location/n";

Essex

Orchard



Methods, however, can't be defined after an object is instantiated.

It is useful to create objects of stdClass when you have a function that takes a generic object, such as one returned from a database fetching function, but you don't want to actually make a database request. For example:



function pc_format_address($obj) {

return "$obj->name <$obj->email>";

}

$sql = "SELECT name, email FROM users WHERE id=$id";

$dbh = mysql_query($sql);

$obj = mysql_fetch_object($dbh);

print pc_format_address($obj);

David Sklar <david@example.com>



The pc_print_address( ) function takes a name and email address and converts it to a format as you might see in the To and From fields in an email program. Here's how to call this function without calling mysql_fetch_object( ):



$obj = new stdClass;

$obj->name = 'Adam Trachtenberg';

$obj->email = 'adam@example.com';

print pc_format_address($obj);

Adam Trachtenberg adam@example.com
google


In case anybody didn't catch it, stdClass is the base php class, form which all other classes are actually extended. This gives the basics of default methods and properties, and is an inherint concept in class type data structures.



The reason you see it so much in mambo is becuase mambo developers have decided to use stdClass type objects as configuration/information repositories instead or parrallel variables or arrays. These objects are used exclusively for run time storage of variables (just for their set and get abilities.)



They actually get quite ugly if you turn your php alerts on (as most of us do when we develop,) as you can see an alert for each call to a variable that has yet to be set.



This is actually a beef of mine here. The use of these empty objects, with attributes set at runtime (as opposed to being declared as a part of the class) generate a few problems:



1) the only bug I've ever found in PHP4 was with respect to refering undeclared object properties which used to trip out php4, and give you an error relating to a completely different bit of code - although that might have required the properties to be returned by reference, I can't rememeber.



2) you can never really see what the object is like/supposed to be like if it isn't set up properly. Similarily the object can never be tested for vailidty before being passed on.



3) all occurences of such objects are completely non-differentiable (sp?) You can't tell if two are meant for the same purpose or if they are for different purposes.



I'm not much of a programmer but I think it would be much better to use a base class defined for this use, which has some understanding of what it stores and can perhaps self-validate and self manage (and print itself out.)



I wrote a php4 object of such a nature which I called a struct. It is an abstracted class, which when extended is told what kind of information it holds. It then spend it's existence setting and retrieving the values it is meant to hold, in essence serving it's purpose an a configuration information repository. In addition it can self-validate ( $obj->isValid(); ), return meta inforamtion on itself and it's variables and .... that's it actually.

Of course my code is made irrelevant by php5 which actually handles the sets and gets much better (but still would require a base abstract class.)



ok - enough of my blab.spa



補充一下

stdClass在PHP5纔開始被流行。而stdClass也是zend的一個保留類。stdClass是PHP的一個基類,
全部的類幾乎都繼承這個類,因此任什麼時候候均可以被new,可讓這個變量成爲一個object。同時,
這個基類又有一個特殊的地方,就是沒有方法。凡是用new stdClass()的變量,
都不可能會出現$a->test()這種方式的使用。PHP5的對象的獨特性,對象在任何地方被調用,
都是引用地址型的,因此相對消耗的資源會少一點。在其它頁面爲它賦值時是直接修改,而不是引用一個拷貝
相關文章
相關標籤/搜索