什么是PHP中的stdclass()
什么是PHP中的stdclass() 看到有人用这个,不知道为何,找到如下文章解释: 中文的,应用自http://www.stud.uni-karlsruhe.de/~uu5i/blog/index.php?aid=261 php的stdClass是什么 简述 这两天看drupal的代码,发现他常用这个类 可是查了整个文件也没找到stdCl
什么是PHP中的stdclass()
看到有人用这个,不知道为何,找到如下文章解释:
中文的,应用自http://www.stud.uni-karlsruhe.de/~uu5i/blog/index.php?aid=261
php的stdClass是什么
简述
这两天看drupal的代码,发现他常用这个类
可是查了整个文件也没找到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.
这篇文章还引用到另一篇英文的文章,对stdclass做了详细解释:
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
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.