PHP mysqli_result : Use Traversable interface with fetch_object

标签 php mysqli

PHP mysqli_result 类实现 Traversable 接口(interface)已有一段时间了。 这提供了一些有趣的可能性,例如以下代码:

<?php

$db = new mysqli( '...' );

$result = $db->query( '...' );

foreach( $result as $row ) {
   //$row is an associative array containing the fetched values
}

现在,我非常喜欢这个,但是在我目前正在处理的库中,我希望获取对象,而不是关联数组。我还希望能够将一个类传递给它,这样我就可以用它创建一个简单的模型系统

为此,我这样扩展它:

<?php

class mysqli_result_extension extends mysqli_result {

    protected $result_class;

    public function set_result_class( $class ) { $this->result_class = $class; }

    public function function_to_overwrite() {

        return $this->fetch_object( $this->result_class );
    }
}

我的问题和我想问你的问题:

现在如何从 fetch_assoc 切换到 fetch_object? 我想覆盖 Traversable 接口(interface)使用的方法以返回结果,但它没有定义任何 ( http://php.net/manual/en/class.traversable.php )

那么我需要覆盖什么才能从 fetch_assoc 切换到 fetch_object 并能够将我的 $result_class 传递给它,这样我就可以做这样的事情:

<?php

$db = new mysqli_extension( '...' );

$posts = $db->query( 'select * from posts' );
//$result is object of type mysqli_result_extension

$posts->set_result_class( 'post' );

foreach( $posts as $post ) {
   //$post is an instance of class 'post'
}

我不想要一些脏东西,比如覆盖 fetch_assoc 并只返回其中的对象

在此先感谢您的帮助和意见

最佳答案

mysqli_result 的(让我们称之为)遍历获取模式 不能受影响。所以默认情况下它是关联数组(列名作为键,它的值作为值)像mysqli_result::fetch_assoc()它保持不变。

所以这需要一个自己的 Iterator做的工作。那么传统上 Mysqli 结果的非 foreach 迭代是什么样子的呢?对了,while()循环:

while($row = $result->fetch_assoc())
{
   ...
}

其中显示:虽然获取方法不返回 NULL继续。这是创建迭代的常见形式。

对于这种迭代,我通常使用 FetchingIterator , 它是一个具体的迭代器,将 PHP 特定的 Iterator界面(倒带、有效、当前、关键、下一个)变成更简化的界面。简化的一个是:倒带下一个,下一个下一个,直到 NULL 有效。 - 这与 while 几乎相同循环并可用于类似的工作 API,如已弃用的 mysql 甚至数组(比较 array_shift 等)。它也类似于不同编程语言中迭代器的其他实现,这里特别是 Java(比较 Iterator Design Pattern (Source Making Guide on Design Patterns) article )。

我去年在我的文章 Some PHP Iterator Fun (Feb 2012) 中做了基本概述一个大大改进的标准实现可以在 FetchingIterator in Iterator Garden 中找到。这很容易允许做你想做的事。

让我们看一个示例应用程序:

class ObjectFetchIterator extends FetchingIterator
{
    public function __construct(mysqli_result $result, $class) {

        parent::__construct(function() use ($result, $class) {
            return $result->fetch_object($class);
        });
    }
}

mysqli_result::fetch_object()以前在 while(...) 中的方法现在已移入 FetchingIterator 的构造函数中 用于 mysqli 结果。这是一个非常直接的实现。或者,也可以覆盖 protected fetchNext()方法,但我只是注意到它,因为这里不需要它。

用法示例非常简单:

$db       = new mysqli('...');    
$result   = $db->query('...');    
$iterator = new ObjectFetchIterator($result, 'myClass');

/* @var $row myClass */
foreach ($iterator as $row)
{
   // $row is a myClass now based on fetched values
}

正如这个例子所示,没有必要为此使用 PDO,这听起来像是一个非常懒惰的借口:仅为这个小细节更改整个数据库层?不!相反的情况是:在 foreach 中使用迭代器允许您将代码从具体的数据库访问库解耦到泛型类型(因为之前也可以使用数组,但是到目前为止,数组与对象类型没有那么明显)。

希望这有助于即使答案来得有点晚。刚刚引起我的注意,到目前为止还没有真正回答。

关于PHP mysqli_result : Use Traversable interface with fetch_object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13417944/

相关文章:

php - 对象中的Mysql查询

php - 抓取页面然后计算某个类的 div 数量并回显该数字

php - 变量数量与参数数量不匹配——是的,它们匹配

php - 连接到旧的 MySQL 服务器

php - Elasticsearch 空间

php - 在 Windows 上安装后无法运行 pear 命令

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

PHP mysql_stmt::fetch() 给出 PHP fatal error 内存耗尽

php - mySQL 组记录具有来自子表的最新条目

php - 需要从mysql中的2个表中获取数据