Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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_Oop - Fatal编程技术网

Php 真实世界抽象类用法简单示例

Php 真实世界抽象类用法简单示例,php,oop,Php,Oop,有使用抽象类的真实简单示例吗? 我正在尝试使用PHP的OOP,但我仍然不明白-为什么应该使用抽象类以及何时使用(是的,我知道不可能创建抽象类实例,只有继承它的类的实例)。也许你有一个image类,你有两个驱动程序,GD和ImageMagick 您的基类可能是 abstract class Image { public function resize($width, $height) { // Some prep code... } } 那么您的GD驱动程序将是

有使用抽象类的真实简单示例吗?
我正在尝试使用PHP的OOP,但我仍然不明白-为什么应该使用抽象类以及何时使用(是的,我知道不可能创建抽象类实例,只有继承它的类的实例)。

也许你有一个image类,你有两个驱动程序,GD和ImageMagick

您的基类可能是

abstract class Image {

    public function resize($width, $height) {
        // Some prep code...
    }
}
那么您的GD驱动程序将是

class Image_Gd extends Image {
    public function resize($width, $height) {
        // Actual code
    } 
}

看。它有一个
Image
类,它是
abstract

好的,假设您想加载脚本配置。该配置可以存储在database/XML/INI/YAML文件中。每个驱动程序都必须实现一些接口,我们称之为
ConfigurationLoader

interface ConfigurationLoader {
    public function load();
    public function get($key, $default = null);
}

// The final usage of the code:

$configuration = new XMLConfiguration('./some/file.xml');
$configuration = new YAMLConfiguration('./some/file.yaml');
$configuration = new DatabaseConfiguration($dbHandler, 'table_name');

$configuration->load();
echo $configuration->get('abc.def.ghi');
所以现在我们需要实现我们的驱动程序。您应该注意的第一件事是,基于文件的驱动程序可能工作相同。唯一的区别是它们各自以不同的方式解析文件源。另一方面,数据库驱动程序的工作方式完全不同

class DatabaseConfiguration implements ConfigurationLoader {
    protected $config = array();

    protected $pdo, $tableName;

    public function __construct(PDO $dbHandler, $tableName) {
        $this->pdo = $pdo;
        $this->tableName = $tableName;
    }

    public function load() {
        $this->config = /* fetch data from database */;
    }

    public function get($key, $default = null) {
        return array_key_exists($this->config, $key) ? $this->config[$key] : $default;
    }
}
现在我们需要实现XML、INI和YAML驱动程序。它们的工作原理几乎相同,所以。。。让我们创建一些抽象类来处理公共代码:

 abstract class FileConfiguration implements ConfigurationLoader  {
      // Each file-based configuration loader has a constructor that takes file name as first argument
      protected $filename;

      public function __construct($filename) {
          $this->filename = $filename;
          $this->getFileContents();
      }

      // Each file-based driver has to store file content
      protected $fileContent;

      protected function getFileContents() {
          $this->fileContents = file_get_contents($this->filename);
      }

      // Each driver will have to implement its own implementation of load().
      // XMLConfiguration will parse XML, INIConfiguration will parse INI etc. 
      abstract public function load();

      // However all of them will store parsed configuration in $config array:
      protected $config = array();

      public function get($key, $default = null) {
          return array_key_exists($this->config, $key) ? $this->config[$key] : $default;
      }
 }
FileConfiguration
必须是抽象的,因为它不知道如何解析文件内容。只有交付的类才知道如何做到这一点:

 class XMLConfiguration extends FileConfiguration {
      public function load() {
          $xml = simplexml_load_string($this->fileContents);

          foreach ($xml->root as $config) {
              $this->config[(string) $config->key] = (string) $config->value;
          }
      }
 }

 class YAMLConfiguration extends FileConfiguration {
      public function load() {
          $yaml = new SomeYAMLParser($this->fileContents);

          foreach ($yaml->parse() as $config) {
              $this->config[$config['key']] = $config['value'];
          }
      }
 }

 class INIConfiguration extends FileConfiguration {
      public function load() {
          ...
      }
 }

正如您在本例中看到的,甚至还有一个抽象类
AbstractConfiguration
,它将存储
$config
属性和
get($key,$default=null)
方法,它将是
数据库配置
文件配置
的父类,我认为创建
接口映像
并使
映像(实现映像
)就足够了?@Kirzilla是的,你可以做到。想象一下这个基类中有代码,最好不要看Kohana的源代码。这不是OOP设计的最好例子。@Crozin很公平-你推荐什么?