当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Making FETCH_CLASS dynamic

问题描述:

Thanks for reading. How does one make the class name dynamic in a FETCH_CLASS query. i.e. so that this method can be called by any class...(what do you replace 'User' with)

 public static function find_by_sql($sql){

global $handler;

$query = $handler->query($sql);

$object_array = $query->fetchAll(PDO::FETCH_CLASS, 'User');

return $object_array;

}

Solution (embarrassingly) is....

 public static function find_by_sql($sql){

global $handler;

$query = $handler->query($sql);

$class = get_class($this);

$object_array = $query->fetchAll(PDO::FETCH_CLASS, $class);

return $object_array;

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: