|  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
Have you experienced this issue?
Rate the importance of this bug to you:

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


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')) { 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 23:45 UTC]
-Package: Feature/Change Request +Package: MySQL related
 [2011-04-18 14:10 UTC]
-Status: Open +Status: Wont fix
 [2011-04-18 14:10 UTC]
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-2023 The PHP Group
All rights reserved.
Last updated: Tue Dec 05 06:01:28 2023 UTC