What is the Stdclass () ____php in PHP

Source: Internet
Author: User
Tags data structures php class

What is Stdclass in PHP ()

See someone with this, don't know why, find the following article to explain:

Chinese, applied from http://www.stud.uni-karlsruhe.de/~uu5i/blog/index.php?aid=261


What is the Stdclass of PHP?

Briefly

These two days to see the code of Drupal, found that he used this class

But the whole file was checked and no definition of stdclass was found. Estimate is a built-in object, check the manual. The manual found that Stdclass is a class reserved by Zent. That's all.



Google Chinese a check, mostly PHP manual on the unified sentence. Later saw Maboo China on the question, which lang3 unexpectedly answer let others make good use of the resource manager. It's so unkind.



Google search all languages. A little information was finally found.

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

The original is the base class. Many PHP programmers use it to pass a series of variable values, while at the same time lazy to create their own class.

This base class can only pass properties, not define methods. Because once a class is materialized, you cannot add a method.

To be clear, this stdclass is similar to the Structur in C + +. You can use it to store many variable attributes, but there is no method. That's it.

StdClass is a default PHP object.


This article also refers to another article in English, the Stdclass did a detailed explanation:


This is 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 are 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 are want to create the object and add properties to it, but you don ' t want to formally define it

As a specific class. This is useful the have a function that requires the 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 a empty array, creating an object of type StdClass provides

You have an object without properties or methods.

Like objects belonging to the other classes, 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 a object is instantiated.

It is useful to create objects the StdClass when your 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 From fields in a 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 are the base PHP class, form which all other classes are. This gives the basics of default methods and properties, and is a inherint concept in class type data structures.



The reason you'll be becuase Mambo developers have decided to use StdClass type objects as Configurat Ion/information repositories instead or parrallel variables or arrays. These objects are used exclusively for run time storage of of variables (just for their set and get abilities.)



They actually get quite ugly if your turn your PHP alerts on (as most to us do we develop,) as can R each call to a variable which has yet to is set.



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



1) The only bug I ' ve ever found in PHP4 is and respect to refering undeclared object properties which used to trips out P HP4, and give you an error relating to a completely different bit of code-although that might have required the Properti Es to is returned by reference, I can ' t rememeber.



2 You can never really the "object is like/supposed" to "like if it isn" t set up properly. Similarily the object can never be tested to vailidty before being on.



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



I ' m not so much of a programmer but I I'm the it would is 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 a 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 C Onfiguration 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 are 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.

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.