php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45276 feature request: mysql_fetch_object_dynamic
Submitted: 2008-06-15 19:39 UTC Modified: 2011-04-18 14:10 UTC
From: darrel dot opry at gmail dot com Assigned:
Status: Wont fix Package: MySQL related
PHP Version: 5.3CVS-2008-06-15 (CVS) OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: darrel dot opry at gmail dot com
New email:
PHP Version: OS:

 

 [2008-06-15 19:39 UTC] darrel dot opry at gmail dot com
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();
}



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 23:45 UTC] jani@php.net
-Package: Feature/Change Request +Package: MySQL related
 [2011-04-18 14:10 UTC] johannes@php.net
-Status: Open +Status: Wont fix
 [2011-04-18 14:10 UTC] johannes@php.net
It is hard to do properly in a generic way which works for most cases. Which we have to do at the level. You can build an iterator or such doing what you need quite efficiently.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 14:01:28 2024 UTC