什麼是PHP中的stdclass()____PHP

來源:互聯網
上載者:User

什麼是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.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.