Re: Extending PEAR DB
| From: | (Stig Sæther Bakken) | Date: | Thu, 26 Apr 2001 09:23:44 +0000 |
| Subject: | Re: Extending PEAR DB | ||
| References: | 1 2 3 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-324@lists.php.net to get a copy of this message | ||
["Benjamin R. Webb" <brwebb@transmuto.com>]
> > $obj = (object)$result->fetchRow();
>
> > That being said, having a fetch mode for objects could be very useful
> > if you can specify what class or object to use.
>
> > - Stig
>
> > --
> Stig,
>
> Did I understand correctly that you want to be able to cast a query
> result to an arbitrary object? I've been working out a way to do
> that in my head, but I don't want to put it to code if I misunderstood.
>
> I'm still not sure that we shouldn't put this in a seperate set of functions
> as it will break the consistant array return type. Thoughts?
You can cast an array to an object and convert assoc key/value pairs
to properties. Example:
<?php
var_dump((object)null);
var_dump((object)42);
var_dump((object)"blah");
var_dump((object)array("foo" => 1, "bar" => 2));
?>
Output:
object(stdClass)(2) {
}
object(stdClass)(1) {
["scalar"]=>
int(42)
}
object(stdClass)(1) {
["scalar"]=>
string(4) "blah"
}
object(stdClass)(2) {
["foo"]=>
int(1)
["bar"]=>
int(2)
}
In the example above there are two special return values, an error
object or NULL. The cast won't change the error object of course, but
the NULL value will change to an empty object that you can test with
"sizeof(get_object_vars($o)) == 0".
- Stig
--
Stig Sæther Bakken <ssb@fast.no>
Fast Search & Transfer ASA, Trondheim, Norway