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
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Sun Dec 19, 2010 4:02 pm
You just want to make sure the format of the object is what you want it to be?
eyals
Forum Newbie
Posts: 3 Joined: Sun Dec 19, 2010 3:45 am
Post
by eyals » Sun Dec 19, 2010 4:50 pm
exactly.
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Sun Dec 19, 2010 5:39 pm
You could do a recursive comparison function like this:
Code: Select all
function validateObject($object, stdClass $pattern) {
if (!is_object($object) || !($object instanceof stdClass)) {
return false;
}
foreach ($pattern as $key => $property) {
if (!isset($object->$key)) {
return false;
} else {
if (is_object($property)) {
return validateObject($object->$key, $property);
}
}
return true;
}
}
Not tested. Just pass it the object, and an object to compare against.
eyals
Forum Newbie
Posts: 3 Joined: Sun Dec 19, 2010 3:45 am
Post
by eyals » Mon Dec 20, 2010 12:11 am
It's more complicated than that.
See the first link I posted here.
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Mon Dec 20, 2010 12:59 pm
Oh, I see. Hm, sorry, I don't know of any projects that do that. If you find one, please let us know though.