Due in large part to Drupal and how much I love it, I enrolled myself in a PHP class at the local community college this semester so I could really understand Drupal better.

Throughout the code I am seeing the stdClass instantiated with new stdClass() of course. However, I can not find in the code where stdClass is defined. I'm looking for something like this:

class stdClass
{
definition goes here
}

Can someone tell me where to find this, or if it does not exist, why not? If it does not exist, maybe a link to something to read so you're not all trying to give me a PHP lesson?

Thanks for taking the time to answer this. I'm loving my PHP class and doing well in it. Hopefully someday I'll be able to give something back to Drupal!

Comments

nevets’s picture

stdClass is defined by PHP, that is it is builtin to PHP.

jscoble’s picture

stdClass is the base class for PHP.

$foo = new stdClass();
$foo->bar = 'bar';
$foo->hello = 'hello world'; 
legend112’s picture

on this note, if it'll be of help to anyone in the future, i was stuck in the same mudpool and i've learnt a StdClass is NOT necessarily a base class. it helps to think of it as an empty class.

this is easily proved by this code:

class $ball {}
$a  = new $ball();
echo ($a instanceof StdClass)?'Y':'N';

//should output N

kriskd’s picture

Keeping in mind there is nothing I'm trying to do with this, just learn more about it.

With that said where can I find (ie, documentation) what variables and functions are defined in it and how to use it?

jscoble’s picture

php.net

b.ravanbakhsh’s picture

it is Predefined Classes since PHP 5. Simply Say it is available class provided
Example #1

//Using get_declared_classes() PHP function whitch Returns an array with the name of the defined classes.
print_r(get_declared_classes());
//output:
array([0]=>stdclass,
[1]=>...)

stdclass Usage #1:when you want to create an object, and assign data to it, without having to formally define a class:

$myobject = new stdClass;
$myobject->nid = 20;
$myobject->title = "Something";
$myobject->value = "Something else";

stdclass Usage #2:another usage of stdclass is when using casting other types to object:

 $arr = array(
    "nid"=> 20,
    "title" => "Something",
    "value" => "Something else"
);
$node = (object) $arr;
var_dump($node);

b.ravanbakhsh@gmail.com