我需要一个包装类的解释,其中包含一个孩子会理解的例子

dar*_*a33 15 php

我读了整篇文章.它描述了包装类对其他有经验的程序员来说是什么,而不是像我这样的新手.

我理解PHP语法,面向对象的概念,但还没有在面向对象的代码中编写我自己的应用程序.在试图弄清楚包装类究竟是什么时,我感到很困惑.我不懂技术术语.

我希望有人以一个漂亮的详细的孩子般的描述回答,这个描述对于理解面向对象程序的基础知识的人来说很容易理解,并且已经阅读了几乎所有的php.net语言参考,但是没有实际的面向对象编程经验.尚未编写任何申请.

sbe*_*rry 20

由于你所关联的问题的解释非常广泛,我不会再为你重新定义它.相反,我会尝试通过注射示例向您展示.

class Logger {

    private $__logger;

    public function __construct($logger) {
        $class = $logger . "Logger";
        $this->$__logger = new $class();
    }

    public function write($message) {
        $this->$__logger->write($message);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,上面你有一个类Logger,你可能会用它来记录某个地方的信息.我们并不关心它是如何做到的,我们只是知道它做到了.

现在,我们有几种不同的伐木可能性......

class DBLogger {

    public function write($message) {
        // Connect to the database and 
        // INSERT $message
    }
}

class FileLogger {

    public function write($message) {
        // open a file and
        // fwrite $message
    }
}

class EMailLogger {

    public function write($message) {
        // open an smtp connection and 
        // send $message
    }
 }
Run Code Online (Sandbox Code Playgroud)

现在,当我们使用记录器时,我们通过执行以下任何操作来执行此操作:

$logger = new Logger("DB");
$logger = new Logger("EMail");
$logger = new Logger("File");
Run Code Online (Sandbox Code Playgroud)

我们总是以$logger相同的方式进行互动(即我们称之为write($message)).包装器实例Logger包装实际的日志记录类并代表我们调用其方法.

上述类型代码的更常见用法是使用配置文件来确定记录器的内容.例如,考虑您希望将日志记录发送到文件的情况.您可能有一个如下所示的配置:

$logging = array( 
               'type' => 'file',
               'types' => array(
                   'file' => array(
                       'path' => '/var/log'
                       'name' => 'app_errors.log'
                    ),
                    'email' => array(
                       'to' => 'webmaster@domain.com',
                       'from' => 'error_logger@domain.com',
                       'subject' => 'Major fail sauce'
                    ),
                    'db' => array(
                        'table' => 'errors',
                        'field' => 'error_message'
                    )
                )
);
Run Code Online (Sandbox Code Playgroud)

你的改编课程可能如下:

class FileLogger {

    public function __construct() {
        // we assume the following line returns the config above.  
        $this->config = Config::get_config("logger");
    }

    public function write($message) {
        $fh = fopen($this->config->path . '/' . $this->config->file);
        fwrite($fh, $message . "\n");
        fclose($fh);
    }
}
Run Code Online (Sandbox Code Playgroud)

我们会对其他adapted类做同样的事情.然后,通过对主Wrapper的一点修改Logger,我们可以使用配置数据创建正确的包装实例,并将其基于type配置中定义的实例.一旦你有了类似的东西,切换到通过电子邮件记录就像更改type配置一样简单.


Mik*_*e B 5

包装类只是让他们的子类在特定情况下更容易使用。编写了许多库以涵盖各种情况,因此使用起来变得繁琐和复杂。很多项目都会为库创建一个包装器,以使其更易于使用。

以这个 PDO 类用法为例:

$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
Run Code Online (Sandbox Code Playgroud)

不要太在意细节。只要知道这些代码行查询数据库。包装类允许与上面相同的功能,例如:

Db::query('SELECT name, colour, calories
  FROM fruit
  WHERE calories < :calories AND colour = :colour',
  array($calories, $colour));
Run Code Online (Sandbox Code Playgroud)

我们已经将子类的一些繁琐但很少使用的功能移除到一个易于使用的界面中。在内部,包装类正在构造对 PDO 的相同类型的调用,但这对开发人员是隐藏的。

免责声明:这些例子只是……例子。并不意味着展示最佳实践,甚至不实用。