【发布时间】:2011-01-13 04:00:16
【问题描述】:
我想根据 MySQL 数据库中的字符串定义的类型在 PHP 中创建一个对象。数据库表有以下列和样本数据:
id | type | propertyVal
----+------+-------------
1 | foo | lorum
2 | bar | ipsum
...使用 PHP 数据类型
class ParentClass {...}
class Foo extends ParentClass {private $id, $propertyVal; ...}
class Bar extends ParentClass {private $id, $propertyVal; ...}
//...(more classes)...
仅使用一个查询,我想通过 id 选择一行并创建一个类型定义表的类型列的对象,并将 SELECTed 行中的其他列分配给新创建的对象。
我在想使用:
mysql_fetch_object()- 读取类型属性
- 使用由 type 属性定义的类型创建对象
但不知道如何根据字符串动态创建类型。如何做到这一点?
【问题讨论】: