|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[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
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 02:00:01 2025 UTC |
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(); }