Re: [PHP-DEV] Re: RfC: *_fetch_object()

2002-11-09 Thread Timm Friebe
On Fri, 2002-11-08 at 23:41, Yasuo Ohgaki wrote:
 I don't mind making pg_fetch_object() accept object (not class)
 optionally and initialize field values as object's properties.

What you're saying would be:

$a= pg_fetch_object($q, new Article());

It is certainly not common for PHP functions to accept new Something()
as a parameter. Although it is sexy, does it make sense to users? Why
would a fetch function accept an instance of an object? Just my .02
EUR:-)

 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.

I've done away with them. If anyone for any reason still wants them,
(object)sybase_fetch_array($q) is the way to go.

- Timm



-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DEV] Re: RfC: *_fetch_object()

2002-11-08 Thread Yasuo Ohgaki
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