From:             darrel dot opry at gmail dot com
Operating system: 
PHP version:      5.3CVS-2008-06-15 (CVS)
PHP Bug Type:     Feature/Change Request
Bug description:  feature request: mysql_fetch_object_dynamic

Description:
------------
Current you can specify a class name for mysql_fetch_object to instantiate
a class in the request. For some of my applications 
I do not know the proper class at the point I'm querying the 
database. Normally this happens when working with child classes
that have identical database data requirements as their parents.

Currently I use factory functions that query the database and instantiate
the proper class based on the row object, and pass the 
row object into the constructor to initialized the object.

It would be nice to be able to specify a table column that contains the
class name for an object. Something along the lines of:

/**
 * @param mysql_result $result 
 * @param string $class_column to column of the row containing 
 *               the class name that should be initilized.
 * @param array $ctor_args arguments to pass to the class constructor.
 */
mysql_fetch_object_dynamic($result, $class_column, $ctor_args)


Example Use Case:
Loading child classes from with identical data storage.

Given: 

table strings(id, class, value);

class string {
  $id = 0;
  $value = '';
  
  function set_value($value) {
    if (!$this->validate($value)) return false;
    $this->value = $value;
    return true;
  }
  
  function validate($value) {
    return is_string($value);
  }
   
  function print() {
    print $this->value();
  }

  function save() {
    if ($this->id) {
      mysql_query("UPDATE strings 
                   SET value='$this->value' 
                   WHERE id=$this->id");
    }
    else {
      mysql_query('INSERT INTO {strings} 
                   ($this->is, __CLASS__, $this->value));
    }
  }
}


class email extends string {
  function validate($value) {
    return is_email($value);
  }

  function print() {
    print "<a href='mailto: $this->value'>$this->value</a>\";
  }
}

$result = mysql_query('SELECT * FROM strings');

while($string = mysql_fetch_object_dynamic($result, 'class')) { 
  $string->print();
}



-- 
Edit bug report at http://bugs.php.net/?id=45276&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=45276&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=45276&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=45276&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=45276&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=45276&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=45276&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=45276&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=45276&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=45276&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=45276&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=45276&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=45276&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=45276&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=45276&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=45276&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=45276&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=45276&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=45276&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=45276&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=45276&r=mysqlcfg

Reply via email to