Methods of stdClass?

Can anyone tell me all of the methods and members of stdClass? I’m working on a project that would be benefitted greatly by a map or something similar about PHP’s stdClass. I tried looking at the php and zend websites, but I couldn’t find anything on it except that you can’t name your own class ‘stdClass’.

Anyway, if anyone has this information or can steer me towards it, I would greatly appreciate it.

Thanks a million!

I guess the class has no methods and members. The following shows two empty arrays:


<?php
echo '<pre>';
print_r(get_class_methods('stdClass'));
print_r(get_class_vars('stdClass'));
echo '</pre>';
?>

Wouldn’t you have to instantiate the class before running methods on it?

Hmm well I just instantiated that and ran it and got 1 empty array and an “object to string” conversion error.

So what is up with this Mysterious stdClass?

According to this page it’s just used internally by Zend.

http://www.zend.com/manual/language.oop.php

The name stdClass is used interally by Zend and is reserved. You cannot have a class named stdClass in PHP.

But what’s it being used internally for?

If it’s so important, wouldn’t they at least do something cool with it? I thought it help __sleep() and __wakeup() at least?

Fancy looking through some source code?

Yea, good idea.

I’ll just hit up Chora and see what it has to offer. Hopefully they actually use comments in their code.

I use stdClass sometimes for storing data structures when I don’t want to write my own class for the job.

The easiest way to think of it is as just another data type, like you have array and string. In a more theoretical sense, it’s kind of like PHP’s “root” object with a loose parallel to Java where everything is an object.

You can create instances of it like;


$obj=new stdClass;
$obj->myVar='Hello World';
echo ( $obj->myVar );

In theory I guess you could add methods to it using create_function() - something I’ve never tried.

In theory I guess you could add methods to it using create_function() - something I’ve never tried.

after checking out create_function() I don’t think this is possible, all the examples given depended on the given class having some form for get() method in order to utilise create_function()

It would be nice if, as part of the PHP5 push for a greater object model, all the existing functions were organised into a proper internal class library such as you’d get with Java/C#… Not replacing the existing functions of course, merely aliasing them.

Was thinking something like this;

$obj=new stdClass;

$obj->setVar = create_function ( ‘$arg’, ‘$this->var=$arg;’ );
$obj->getVar = create_function ( ‘’, ‘return $this->var;’ );

$obj->setVar ( ‘Hello World’ );

echo ( $obj->getVar() );



Wierd thing is I can't get create_function to do anything - tried the simple example in the manual in 4.2.3 and 4.3.1 - does nothing.

[quote]
It would be nice if, as part of the PHP5 push for a greater object model, all the existing functions were organised into a proper internal class library such as you'd get with Java/C#...... Not replacing the existing functions of course, merely aliasing them.



Absolutely.

K, so stdClass would mainly be just like writing:


class absClass
{
 
  var $member = array();
 
  __get($prop, &$value)
  {
	if(isset($this->member[$prop]))
	{
	  $value = $this->member[$prop];
	  return true;
	}
	else
	{
	  return false;
	}
  }
 
  __set($prop, $value)
  {
	$this->member[$prop] = $value
	return true;
  }
}

Correct? That would be the equivalent? But just built-in?

So we are pretty much using stdClass just to save time, not to really inherit it or anything?

create_function() just makes the PHP equivalent of a lambda for those who are too lazy to define a prototype for it ( me =] ), how would you add a permanent (well not permanent, but accessible during runtime) method to stdClass with it, because you can’t add methods after instantiation, and create_function() just creates a one-time-use-disposable function, right? Or am I just totally off-base?

Now I know that stdClass as-is with no standard methods or members

First of all;


$obj->setVar ( 'Hello World' );

Ignore that - brain was asleep… (doesn’t work of course because PHP then thinks setVar is a method not a variable).

Correct? That would be the equivalent? But just built-in?

Basically yes - you’d need to overload ( ‘absClass’ ); but that’s basically the same thing.

So we are pretty much using stdClass just to save time, not to really inherit it or anything?

I think the real reason the PHP group built it was for interacting with other systems - this came up here when using SOAP with the Google API. I can’t see away to use stdClass in an inheritance relationship - PHP doesn’t support dynamic casting ( at least with 4.0 ) like Java, so you couldn’t create an stdClass then cast it to something else. Basically stdClass is just a form of array - all the array functions work on stdClass in the same way is they do with arrays.

In general though reckon it’s best not to go overboard with stdClass - personally only use it sometimes for storing data such as when you want an array of objects.