Php 如何使用mysqli::fetch_object()?
人们通常如何将mysqli结果转化为对象 在使用自定义类时,我找不到任何带有fetch_object()的示例。假设以下设置Php 如何使用mysqli::fetch_object()?,php,object,mysqli,fetch,Php,Object,Mysqli,Fetch,人们通常如何将mysqli结果转化为对象 在使用自定义类时,我找不到任何带有fetch_object()的示例。假设以下设置 class Test1 { function __construct(array $data) { ... } } class Test2 { function __construct(array $data) { ... } } 假设我有多个具有相同构造函数的类,如何动态实例化这些类 我会这样想象: function customQuery($que
class Test1 {
function __construct(array $data) { ... }
}
class Test2 {
function __construct(array $data) { ... }
}
假设我有多个具有相同构造函数的类,如何动态实例化这些类
我会这样想象:
function customQuery($query, $class) {
// Get some data by mysqli query, assume we get the data as $result object
...
// Now I would like to store the data in an array with instances of $class
$array = array();
while ($obj = $result->fetch_object($class, ???) {
$array[] = $obj;
}
// Return the array with instances of $class
return $array;
}
class Class_Name
{
public static function customQuery($query)
{
$return = array();
if ($result = $mysqli->query($query)) {
return $result->fetch_object('Class_name');
}
// return false if no results found
return false;
}
}
我用什么作为问号的论据?我只知道Test1和Test2的类构造函数都想要一个关联数组作为输入(长度可能不一样!)
最后,我只想做一些类似的事情
$arr1 = customQuery('SELECT id, product FROM test1 LIMIT 10', 'Test1');
$arr2 = customQuery('SELECT id, name, address FROM test2 LIMIT 10', 'Test2');
当然,如果您有更好的想法来实现我的目标,我将非常感谢您的意见。请看“获取对象”
您有一个参数$class\u name
从文档中:
要实例化、设置和返回的属性的类的名称。如果未指定,则返回stdClass对象
它将自动创建给定类的实例并设置属性,因此无需传递数组
再解释一下
fetch_对象只是填充私有属性等(PHP魔法…我不喜欢它)
如果您的对象在构造函数中具有必需的参数,但您希望从数据库中获取该参数,则fetch_对象允许您为构造函数定义参数,以便可以构造它,而不是抛出警告/错误
class Test {
private $title;
public function __construct($required) {
}
}
$mysql->fetch_object('Test'); //will trigger errors/warnings
$mysql->fetch_object('Test', array(null)); //won't
您可以做的是simpel获取一个数组并构造对象
function query($query, $class) {
$data = $query->fetch_assoc();
$instance = new $class($data);
return $instance;
}
在结果上使用。在($obj=$result->fetch_query()){…}
function customQuery($query) {
if ($result = $mysqli->query($query)) {
while ($obj = $result->fetch_object()) {
# do stuff.
}
} else {
# raise some error, query did not make it.
}
}
如果指定param
,则该类将被实例化以处理结果
# $result->fetch_object('MyClass');
MyClass {
private $id;
public function __construct($id/* fields */) {
$this->id = $id; # say `id` is the only field
}
}
这个功能没有多大用处 最好让对象的构造函数接受带有设置的数组,并像这样使用
while ($row = $result->fetch_assoc($res) {
$array[] = new Foo($row);
}
还要注意,fetch_assoc/fetch_对象并不总是可用于准备好的语句。fetch_对象()方法将返回stdClass的实例。但是,如果指定类名,它将返回该类的实例,并根据返回的数据填充属性。除非要更改其可见性(即受保护或私有),否则不需要指定类中的所有列。否则,它们将默认为公共
此外,最好在要实例化的类中使用静态方法,以整齐地分离关注点。请参阅:
class Class_Name
{
public static function customQuery($query)
{
$return = array();
if ($result = $mysqli->query($query)) {
// not passing a class name to fetch_object()
// will return an instance of stdClass
while ($obj = $result->fetch_object('Class_Name')) {
// add the fetched object (as an instance of the
// 'Class_Name' class in this case) to the return array.
$return[] = $result;
}
return $return;
}
// return false if no results found
return false;
}
}
这样调用静态方法,您将得到一个“Class_Name”对象数组:
$results = Class_Name::customQuery($query);
如果仅查询1个结果,则上述结果如下:
function customQuery($query, $class) {
// Get some data by mysqli query, assume we get the data as $result object
...
// Now I would like to store the data in an array with instances of $class
$array = array();
while ($obj = $result->fetch_object($class, ???) {
$array[] = $obj;
}
// Return the array with instances of $class
return $array;
}
class Class_Name
{
public static function customQuery($query)
{
$return = array();
if ($result = $mysqli->query($query)) {
return $result->fetch_object('Class_name');
}
// return false if no results found
return false;
}
}
有了它,您将得到一个“Class\u Name”对象
注意,如果使用的是命名空间类,请应用完全限定的命名空间类名。以下是正确的使用方法:
<?php
$res = $mysqli->query($q);
while($params = $res->fetch_assoc()):
endwhile;
$res->free();
$res = $mysqli->query($q);
while ($obj = $res->fetch_object('ClassName', array($params)):
$obj_list[] = $obj;
endwhile;
?>
是的,我看到了,但我不明白的是$params在构造函数中的用法。基本上,通过MySQLi接收的所有数据都应该是我想要实例化的对象的params。你不能以这种方式在构造函数中使用查询中的数据。你可以做的是创建一个接口IFetchable或其他什么,然后创建一个method finalizeFetch();wich使用已设置的属性。是的,直到现在我都是这样做的,但我认为可能有更干净的方法