Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP的读写器接口_Php_Interface_Dependency Injection_Software Design - Fatal编程技术网

PHP的读写器接口

PHP的读写器接口,php,interface,dependency-injection,software-design,Php,Interface,Dependency Injection,Software Design,我已经创建了两个名为ReaderInterface和WriterInterface的非常基本的接口,但是我已经从这个示例中删除了WriterInterface,因为没有必要说明我的难题 ReaderInterface.php interface ReaderInterface { public function read(); } class Datatable { protected $cols; protected $rows; protected $re

我已经创建了两个名为ReaderInterface和WriterInterface的非常基本的接口,但是我已经从这个示例中删除了WriterInterface,因为没有必要说明我的难题

ReaderInterface.php

interface ReaderInterface
{
    public function read();
}
class Datatable
{
    protected $cols;
    protected $rows;

    protected $reader;

    public function __construct()
    {
        $this->cols = array();
        $this->rows = array();

        $this->reader = null;
    }

    public function setReader(ReaderInterface $reader)
    {
        $this->reader = $reader;
    }

    public function load()
    {
        //Contents to follow below.
    }
}
我有一个名为Datatable的具体类:

Datatable.php

interface ReaderInterface
{
    public function read();
}
class Datatable
{
    protected $cols;
    protected $rows;

    protected $reader;

    public function __construct()
    {
        $this->cols = array();
        $this->rows = array();

        $this->reader = null;
    }

    public function setReader(ReaderInterface $reader)
    {
        $this->reader = $reader;
    }

    public function load()
    {
        //Contents to follow below.
    }
}
我实例化了一个datatable实例,如下所示:

$db = new PDO("mysql:host=localhost;port=3306;dbname=test", "user", "pass"); //Let's pretend this is a good connection.

$datatable = new Datatable();
$datatable->setReader(new DatatableReader($db));
$datatable->load();
public function load()
{
    if ($this->reader != null) {
        $retval = $this->reader->read();

        //Do stuff with the array of information returned from the reader.
    }
}
我的问题是如何实现DatatableReader,以便它可以从我传入的数据库中读取数据,并写入Datatable对象中的
$this->cols
$this->rows

我马上看到两种方法

1。依赖注入

class DatatableReader implements ReaderInterface
{
    protected $db;
    protected $datatable;

    public function __construct(Datatable &$datatable, PDO &$db)
    {
        $this->datatable = $datatable;
        $this->db = $db;
    }

    public function read()
    {
        //Execute prepared statement which returns 5 records.
        //Get the columns, and place them into an array.

        foreach ($columns as $col) {
            $this->datatable->addColumn($col); //Add a column to the datatable.
        }
    }
}
然后,我的
Datatable::load()
方法将实现为:

public function load()
{
    if ($this->reader != null)
        $this->reader->read();
}
2。从read()返回弱类型。

class DatatableReader implements ReaderInterface
{
    protected $db;

    public function __construct(PDO &$db)
    {
        $this->db = $db;
    }

    public function read()
    {
        //Execute prepared statement which returns 5 records.
        //Get the columns, and place them into an array.
        return $columns;
    }
}
然后,我将调用我的
load()
方法,如下所示:

$db = new PDO("mysql:host=localhost;port=3306;dbname=test", "user", "pass"); //Let's pretend this is a good connection.

$datatable = new Datatable();
$datatable->setReader(new DatatableReader($db));
$datatable->load();
public function load()
{
    if ($this->reader != null) {
        $retval = $this->reader->read();

        //Do stuff with the array of information returned from the reader.
    }
}
问题

  • 在这两个选项中,哪一个是最好的设计
  • 还有第三种选择我忽略了吗

  • 我会选择方案2。使用选项1可以生成一个递归:
    DatatableReader
    包含对象
    Datatable
    ,反之亦然

    选项1的另一个缺点是滥用
    read
    方法来写入另一个对象。 只有具体的实现(
    DatatableReader
    )知道该对象。
    实现接口的所有对象都应该以相同的方式进行反应。

    我选择选项2。使用选项1可以生成一个递归:
    DatatableReader
    包含对象
    Datatable
    ,反之亦然

    选项1的另一个缺点是滥用
    read
    方法来写入另一个对象。 只有具体的实现(
    DatatableReader
    )知道该对象。

    实现接口的所有对象都应该以相同的方式进行响应。

    您应该做的第一件事是将
    Datatable
    重命名为
    Datatable
    ,将
    DatatableReader
    重命名为
    DatatableReader
    @Omar为什么?Datatable是一个复合词,就像接口一样。你不会像Interface那样拼写Interface吧?@crush我不知道哪个是“正确的”,但当你在google
    datatable
    上搜索时,你会注意到每个人都把它作为
    datatable
    。好吧,这很公平。那么我的实际问题呢=}您应该做的第一件事是将
    Datatable
    重命名为
    Datatable
    并将
    DatatableReader
    重命名为
    DatatableReader
    @Omar为什么?Datatable是一个复合词,就像接口一样。你不会像Interface那样拼写Interface吧?@crush我不知道哪个是“正确的”,但当你在google
    datatable
    上搜索时,你会注意到每个人都把它作为
    datatable
    。好吧,这很公平。那么我的实际问题呢=}你的第一点就是为什么我对选项1犹豫不决。你的第二点是相关的,但我可以对方案2说同样的话。由于read的返回类型在PHP中是弱类型的,因此无论在何处实现,它都将是不同的。另外,如上所述,若我返回一个从源读取的数据数组,那个么我必须对该数据进行两次循环:一次从源读取数据,另一次从read()接收数据后将其应用于我的具体类。您可以返回一个迭代器对象。你只需要读一次。在从数据库获取行时,我仍然需要迭代它,然后在迭代迭代器=]时,我仍然在迭代,这是对数据的两次完整迭代,除非我缺少一些东西。您可以从数据库获取方法返回迭代器。下面是一个pdo示例。在
    read
    方法中,始终返回迭代器。你的第一点就是为什么我对选项1犹豫不决。你的第二点是相关的,但我可以对方案2说同样的话。由于read的返回类型在PHP中是弱类型的,因此无论在何处实现,它都将是不同的。另外,如上所述,若我返回一个从源读取的数据数组,那个么我必须对该数据进行两次循环:一次从源读取数据,另一次从read()接收数据后将其应用于我的具体类。您可以返回一个迭代器对象。你只需要读一次。在从数据库获取行时,我仍然需要迭代它,然后在迭代迭代器=]时,我仍然在迭代,这是对数据的两次完整迭代,除非我缺少一些东西。您可以从数据库获取方法返回迭代器。下面是一个pdo示例。在
    read
    方法中,始终返回迭代器。然后只需在迭代器上迭代一次。