What is stdclass () in PHP ()

Source: Internet
Author: User
Tags drupal
What is stdclass () in PHP to see someone using this, do not know why, find the following article explanation: Chinese, application from www.stud.uni-karlsruhe.de ~ Uu5iblogindex. php? What is the stdClass of aid261php? I read drupal's code over the past two days. I found that he often used this class, but I didn't find stdCl after checking the entire file.

What is stdclass () in PHP to see someone using this, don't know why, find the following article explanation: Chinese, application from http://www.stud.uni-karlsruhe.de /~ Uu5i/blog/index. php? Aid = 261 what is the stdClass of php? I read drupal's code in the past two days. I found that he often used this class, but I didn't find stdCl after checking the entire file.

What is stdclass () in PHP ()

If you see someone using this and don't know why, find the following article to explain:

Application self-http://www.stud.uni-karlsruhe.de /~ Uu5i/blog/index. php? Aid = 261.


What is the stdClass of php?

Brief Introduction

These two days I read drupal's code and found that he often uses this class.

However, the stdClass definition is not found in the entire file. It is estimated that it is a built-in object. Check the manual. According to the manual, stdClass is a class retained by zent. That's all?



Google's article is mostly a single sentence in the php manual. Later, I saw someone asking this question in maboo China. Which lang3 actually answered to let others make good use of the resource manager ?? That's not a good deal.



Google queries all languages. Finally, I found some information.

Http://forum.mamboserver.com/showthread.php? T = 936

It turns out to be a base class. Many php programmers use it to pass the value of a series of variables, and they are too lazy to create their own classes.

This base class can only pass attributes, but cannot define methods. Because once the class is listed in real form, the method cannot be added.

To be clear, this stdClass is similar to structur in C ++. You can use it to store many variable attributes, but there is no way. That's it.

StdClass is a default PHP object.


This article also references another article in English to explain stdclass in detail:


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 have 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 trachtenken ';

$ 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 wocould 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 elastic acted 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 wowould require a base abstract class .)



OK-enough of my blab.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.