Where is stdClass defined.

Where is stdClass defined.

by Paul Ritchings -
Number of replies: 5
Ie in what file?

I have tried looking in the documentation and cannot find a definition.

I am using get_record_sql which I have discovred returns a stdClass object not an associative array.

Didn't think this was a big deal. Have looked on the php site just in case. No help there.

I need to get info out of this in order to pass into another function, the return value of which will form the text property of a block.

To answer this myself. OK stdClass is php not moodle. The reason this occurs is because of the following in dmllib.php

/// DIRTY HACK to retrieve all the ' ' (1 space) fields converted back
/// to '' (empty string) for Oracle. It's the only way to work with
/// all those NOT NULL DEFAULT '' fields until we definitively delete them
if ($CFG->dbfamily == 'oracle') {
array_walk($rs->fields, 'onespace2empty');
}
/// End of DIRTY HACK
return (object)$rs->fields;

That object cast is the problem. Why is it needed?
Average of ratings: -
In reply to Paul Ritchings

Re: Where is stdClass defined.

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
It is not a question of need, it is a question of coding style. If you have an object in PHP, you access its fields by doing $obj->property. If you have an array, you access an element as $ary['property']. Frankly it does not make a lot of difference, it is just a question of coding style. Moodle does it the object way - so you had better get used to it. (Oh, and it is two fewer punctuation characters to type each time, so that is nice.)

You can always cast an array to an object or the reverse, if you really need to.
In reply to Tim Hunt

Re: Where is stdClass defined.

by Paul Ritchings -
Yes but surely casting in this way means there's no way to tell what properties are available?
In reply to Paul Ritchings

Re: Where is stdClass defined.

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, don't you know what data the query was going to return? You can always cast back to an array if you really need to. And if you just want to have a look, just stick in a quick call to print_object() which displays any PHP structure on screen nicely formatted.
In reply to Paul Ritchings

Re: Where is stdClass defined.

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Here are some common patterns that should answer your questions. These all assume you might be in some doubt about the contents of the record, which is not normally the case, but does happen. (Particularly with things like $cm where if you're lucky it will have some extra information added that isn't actually in that db table, but if you're unlucky it doesn't and you might need to go get it separately.)

I just typed these without testing them so only fairly sure they're right.

// does a field exist?
if(isset($record->fieldname)) { ... }

// I want the field to exist and have a non-zero, non-null, non-blank-string value
if(!empty($record->fieldname)) { ... }

// I know the field exists but is it null?
if(is_null($record->fieldname)) { ... }

// [relatively rare] Do something generic with each field from the record
foreach((array)$record as $key=>$value) {
// ...
}

// Get me an array listing all the field names
$fields=array_keys((array)$record);

etc etc

Overall I kind of like the way Moodle does it; it's less typing and less fussy punctuation than using array entries. It's logically a little bit cr**py - certainly in Java I would be arguing for 'proper' objects with record.getField('fieldname') - but if you are worried about that you certainly shouldn't be programming PHP in the first place, given that the entire language is a complete logical balls-up from start to finish. smile

--sam

In reply to sam marshall

Re: Where is stdClass defined.

by Paul Ritchings -
I think you saw my problem: Php wierdness ~ just making things up as you go along.

Objects acquiring properties by assignment. Doesn't make sense since an object is a type. Niklaus Wirth et al really would hate it.

I don't - just didn't realise you can to something similar with classes to what you can do with arrays. As you say a complete logical balls-up.