即时通讯实验用一些方法从我的数据库检索数据。 我做了一个抽象类(ppdao),在这个类中,我有一个函数可以根据表名构建选择查询,并将结果转换为对象。PHP如何获得实例化的类名
对于每个表我做一个小文件,如:
class user extends ppdao{
public $table = 'user';
public function __set ($name, $value){
$this->$name = $value;
}
public function __get ($name){
return $this->$name;
}
可以说,我想在阵列中的所有用户对象,我使用下面的函数从我ppdao类:
public static function get_ar_obj($arWhere = array() , $arWhereWay = array(), $order = null){
$class = get_called_class();
$obj = new $class();
$sql = "SELECT * FROM ".$obj->table. $obj->arWheretoString($arWhere);
$res = mysqli_conn::getinstance()->query($sql)->all_assoc();
return $obj->createObjects($res);
}
这工作一切正常,它给了我想要的结果。
现在我改变了__get功能有点成这样:
public function __get ($name){
switch ($name){
case 'oAlbums':
return $this->oAlbums = albums::get_ar_obj($arWhere = array('user_id' => $this->id));
break;
default:
return $this->$name;
break;
}
我以为我想要得到的所有专辑用户拥有,专辑中的桌子上有一个叫user_id的领域,所以我想我” d像这样将m链接在一起。
现在,当我打电话专辑类是这样的:
$userobject->oAlbums
get_called_class()依然采用的用户类别名称,而不是所谓的专辑类的,因此其产生像
SELECT * FROM user WHERE user_id = 63
And it should be SELECT * FROM album WHERE user_id = 63
查询
任何人有任何想法如何我可以得到这个工作?
对不起人们,它不使用get_called_class创建查询,它使用公共$表。得到它通过改变其进入get_called_class变量
这就是现在的工作是结果:
$arUser = user::get_ar_obj($arWhere = array('id' => 1));
$oUser = $arUser[0];
echo '<pre>';
print_r($oUser->oAlbums);
echo '</pre>';
输出:
Array
(
[0] => album Object
(
[table] => album
[data:ppdao:private] =>
[className:ppdao:private] => album
[id] => 2
[name] => My new album 1
[slug] => my-new-album-1
[user_id] => 1
[views] => 0
[datecreated] => 2013/03/23 16:00:43
[location] => Muaha
)
只是出于好奇,我测试过你在[PHP 5.4.4(http://ideone.com/0OEZ9w)(至少我试图重新组合代码它:D) – dbf 2013-04-21 21:26:46