What is stdClass?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abloodworth
Forum Newbie
Posts: 2
Joined: Wed Jul 26, 2006 2:10 pm

What is stdClass?

Post by abloodworth »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am working on code I didn't create and am getting the error: "Fatal error: Call to undefined method stdClass::update() in" when trying to activate the Account::update() method. I believe I should be dealing with an Account object, as in other places the same function used to generate the Account object generates an Account object. I'm not sure what stdClass is, and why the same function could alternatively generate an Account object and a stdClass object. When I print the object out, indeed one is stdClass with only one key-value data pair (accountStatus), and one is the regular Account object with many key-value data pairs. I'll include the code below, but I think the first thing that would help me is understanding when/why a stdClass would be generated instead of an object.

Thanks!
Allison

Both these functions seem to produce a stdClass object unless the Account Status=Active (which is the default), in which case it creates an Account object:

Code: Select all

// Create Account Object
$userData["Account"] = Account::getByID($userData["User"]->account_ID, 'Any');

// Create Account Object
$userData["Account"] = Account::getByID($u->account_ID);
This function seems to produce an Account object:

Code: Select all

// Create Account Object
$userData["Account"] = Account::getByID($userData["User"]->account_ID, 'Inactive');

	/**
	* @return object
	*/
	static function getByID($ID, $AccountStatus='Active')
	{
		$data=array();
		foreach(Account::getList($AccountStatus) as $d)
		{
			if(($d->ID == $ID) AND (($AccountStatus == 'Any') OR ($d->accountStatus == $AccountStatus)))
			{
				return $d;
			}
		}

		return FALSE;
	}

	/**
	* @return array
	*/
	static function getList($accountStatus='Active')
	{
		//If we're using the cache, and it's already filled
		if(USE_CACHE
			AND ($cache = Cache::getInstance())
			AND ($data = $cache->fetch('account')))
		{
			//no action necessary because we successfully pulled
			//the data from cache
		}
		else
		{
			//Pull entire table into memory.
			$Query = "SELECT " .
				"ID," .
				"sponsor_ID, " .
				"Name," .
				"StreetAddress1," .
				"StreetAddress2," .
				"City," .
				"State," .
				"Zip," .
				"Phone," .
				"Fax," .
				"Email," .
				"Website," .
				"accountStatus," .
				"UNIX_TIMESTAMP(DateCreated) AS DateCreated," .
				"UNIX_TIMESTAMP(DateLastUpdated) AS DateLastUpdated " .
				"FROM account ";

			if(!($DatabaseResult = mysql_query($Query)))
			{
				addToLog("Account::getList() failed", LOG_EMERGENCY, __FILE__, __LINE__);
				return(FALSE);
			}

			$data = array();
			while($Row = mysql_fetch_object($DatabaseResult))
			{
				$data[] = Account::getInstanceFromArray($Row);
			}

			if(USE_CACHE)
			{
				$cache = Cache::getInstance();
				$cache->store('account', $data);
			}

		}

		$list = array();
		foreach($data as $d)
		{
			if($d->accountStatus == $accountStatus)
			{
				$list[] = $d;
			}
		}

		return($list);
	}

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by abloodworth on Wed Jul 26, 2006 2:35 pm, edited 1 time in total.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Code: Select all

around the code please
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Where's the getInstanceFromArray() method? Can we see it please? :) Sounds like it's just returning the row which you fetch using mysql_fetch_object() (which has type stdClass).

EDIT | Hmm... actually why is it called getInstanceFromArray() when you pass it an object?

Code: Select all

while($Row = mysql_fetch_object($DatabaseResult))
                        {
                                $data[] = Account::getInstanceFromArray($Row);
                        }
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

stdclass is generated automatically by mysql_fetch_object() I believe. (I only say this because if you do print_r(mysql_fetch_object($results_from_a_query)) it will print stdClass.
abloodworth
Forum Newbie
Posts: 2
Joined: Wed Jul 26, 2006 2:10 pm

Post by abloodworth »

Thanks much for the info about stdClass -- that helps a lot! In the end I got this fixed, though I stopped running into this stdClass after I made some other changes to some function calls (e.g.

Code: Select all

$userData["Account"] = Account::getByID($u->account_ID, 'Pending');
. Here's the getInstanceFromArray method in case anyone's interested:

Code: Select all

/**
	* @static
	* @return object
	*/
	function &getInstanceFromArray($data)
	{
		$object = new Account;
		$object->ID = $data->ID;
		$object->sponsor_ID = $data->sponsor_ID;
		$object->Name = $data->Name;
		$object->StreetAddress1 = $data->StreetAddress1;
		$object->StreetAddress2 = $data->StreetAddress2;
		$object->City = $data->City;
		$object->State = $data->State;
		$object->Zip = $data->Zip;
		$object->Phone = $data->Phone;
		$object->Fax = $data->Fax;
		$object->Email = $data->Email;
		$object->Website = $data->Website;
		$object->accountStatus = $data->accountStatus;
		$object->DateCreated = $data->DateCreated;
		$object->DateLastUpdated = $data->DateLastUpdated;
		return($object);
	}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

stdClass is a skeleton class, a blank canvas if you like, for uses such as below:

Code: Select all

function & array2object (&$data)
{
    if (is_array($data)) {
        $ret = new stdClass;
        foreach ($data as $key => $val) {
            if (is_array($val) {
                $ret->$key = array2object($val);
            } else {
                $ret->$key = $val;
            }
        }
        return $ret;
    } else {
        return false;
    }
}
Post Reply