I don't mind making pg_fetch_object() accept object (not class)
optionally and initialize field values as object's properties.
How about other db module maintainers?
BTW, pg_fetch_object() accepted 3rd optional parameter
for a long time. I've disabled it recently, since having
$obj-1, $obj-2, and so on does not make much sense.
(It can be accessible via. variable variable, but not
directly) Some db modules may be needed to overload behavior
depend on parameter types.
--
Yasuo Ohgaki
Timm Friebe wrote:
Hi,
what does everyone think about adding a second parameter to the
*_fetch_object() functions (sybase_fetch_object, mysql_fetch_object, ...
etc.) which allows users to define a class name which will then be used
instead of "stdClass"?
E.g.:
class Article {
var $article_id, $caption, $text;
}
$q= sybase_query('select * from article where article_id= 1');
if ($q) while ($a= sybase_fetch_object($q, 'Article')) {
var_dump($a);
}
This saves a couple of lines of code in PHP (formerly, you'd either put
all of article's attributes in the constructor, made the constructor
accept an associative array or assigned all of the attributes like this:
$a= new Article();
$a-article_id= $row['article_id'];
$a-caption= $row['caption'];
$a-text= $row['text'];
) all of which seems ugly and looks like unnecessary overhead. Since PHP
is missing a cast working on user-defined types (
while ($o= sybase_fetch_object($q)) {
$a= (Article)$o;
}
) there would be another suggestion to introduce a function cast:
while ($o= sybase_fetch_object($q)) {
$a= cast($o, 'Article');
}
Though maybe the name isn't ideal and could be implemented in userland,
too (
function cast($var, $type) {
// [...Handle non-objects and normal types...]
// [...Handle non-existant classes w/ name $type...]
$ret= new $type();
foreach (get_object_vars($var) as $k= $v) {
$ret-$k= $v;
}
return $ret;
}
) but with more performance issues than if it was built-in. Maybe it's
even simpler to allow (MyType)$var syntax, but this would have to be
introduced into Zend Engine.
Thoughts?
--
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php