<legend id='a5nBv'><style id='a5nBv'><dir id='a5nBv'><q id='a5nBv'></q></dir></style></legend>
<tfoot id='a5nBv'></tfoot>
<i id='a5nBv'><tr id='a5nBv'><dt id='a5nBv'><q id='a5nBv'><span id='a5nBv'><b id='a5nBv'><form id='a5nBv'><ins id='a5nBv'></ins><ul id='a5nBv'></ul><sub id='a5nBv'></sub></form><legend id='a5nBv'></legend><bdo id='a5nBv'><pre id='a5nBv'><center id='a5nBv'></center></pre></bdo></b><th id='a5nBv'></th></span></q></dt></tr></i><div id='a5nBv'><tfoot id='a5nBv'></tfoot><dl id='a5nBv'><fieldset id='a5nBv'></fieldset></dl></div>

      <small id='a5nBv'></small><noframes id='a5nBv'>

      • <bdo id='a5nBv'></bdo><ul id='a5nBv'></ul>
    1. 如何使用 PHP 抽象?

      时间:2023-05-30
    2. <small id='xIFgY'></small><noframes id='xIFgY'>

        1. <tfoot id='xIFgY'></tfoot>

            <legend id='xIFgY'><style id='xIFgY'><dir id='xIFgY'><q id='xIFgY'></q></dir></style></legend>
              <tbody id='xIFgY'></tbody>

            <i id='xIFgY'><tr id='xIFgY'><dt id='xIFgY'><q id='xIFgY'><span id='xIFgY'><b id='xIFgY'><form id='xIFgY'><ins id='xIFgY'></ins><ul id='xIFgY'></ul><sub id='xIFgY'></sub></form><legend id='xIFgY'></legend><bdo id='xIFgY'><pre id='xIFgY'><center id='xIFgY'></center></pre></bdo></b><th id='xIFgY'></th></span></q></dt></tr></i><div id='xIFgY'><tfoot id='xIFgY'></tfoot><dl id='xIFgY'><fieldset id='xIFgY'></fieldset></dl></div>
              • <bdo id='xIFgY'></bdo><ul id='xIFgY'></ul>
                本文介绍了如何使用 PHP 抽象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                你为什么要用这样的抽象?它会加快工作速度还是它的用途是什么?

                Why would you use such abstract? Does it speed up work or what exactly its for?

                // file1.php
                abstract class Search_Adapter_Abstract {
                    private $ch = null;
                    abstract private function __construct()
                    {
                    }       
                    abstract public funciton __destruct() { 
                      curl_close($this->ch);
                    }
                    abstract public function search($searchString,$offset,$count);
                }
                
                // file2.php
                include("file1.php");
                class abc extends Search_Adapter_Abstract
                {
                    // Will the curl_close now automatically be closed?
                
                }
                

                这里扩展抽象的原因是什么?让我很困惑.我现在能从中得到什么?

                What is the reason of extending abstract here? Makes me confused. What can i get from it now?

                推荐答案

                您可以使用抽象类来定义和部分实现扩展类应该执行的常见任务.由于没有例子很难解释,考虑这个:

                You can use abstract classes to define and partially implement common tasks that an extended class should do. Since explaining it is difficult without an example, consider this:

                如果没有抽象类,您将不得不定义两个具有相同方法和实现的基本类.由于 OOP 完全是为了防止代码重复,这是完全错误的:

                Without abstract classes, you would have to define two basic classes with the same methods and implementation. Since OOP is all about preventing code duplication, this is quite wrong:

                class Car {
                  public $brand = 'mercedes';
                
                  public function gasPerMile($weight) 
                  {
                    // Useless calculation, purely for illustrating
                    $foo = $weight * 89 / 100;
                    return $foo;
                  }
                
                  public function carSpecificFunction() 
                  {
                    // Only present in class Car
                  }
                }
                
                class Truck {
                  public $brand = 'MAN';
                
                  public function gasPerMile($weight) 
                  {
                    // Useless calculation, purely for illustrating
                    $foo = $weight * 89 / 100;
                    return $foo;
                  }
                
                  public function truckSpecificFunction() 
                  {
                    // Only present in class Truck
                  }
                }
                

                现在你有一些通用的属性和方法,它们在两个类中重复.为了防止这种情况,我们可以定义一个抽象类,从它扩展 CarTruck.这样,通用功能就保存在一个地方,扩展类将为卡车或汽车实现特定的属性和方法.

                Now you have some common properties and methods, which are duplicated in two classes. To prevent that, we could define an abstract class from which Car and Truck are extended. This way, common functionalities are kept in one place and the extended classes will implement specific properties and methods for either the Truck or the Car.

                abstract class Vehicle {
                  abstract public $brand;
                
                  public function gasPerMile($weight) 
                  {
                    // Useless calculation, purely for illustrating
                    $foo = $weight * 89 / 100;
                    return $foo;
                  }
                }
                

                通过这种方式,您可以确保至少每个扩展 Vehicle 的类都必须指定一个品牌,并且所有扩展类都可以使用通用的 gasPerMile() 方法.

                This way, you ensure that atleast every class that extends Vehicle has to have a brand specified and the common gasPerMile() method can be used by all extended classes.

                当然,这是一个简单的例子,但希望它能说明抽象类为何有用.

                Of course, this is a simple example, but hopefully it illustrates why abstract classes can be useful.

                这篇关于如何使用 PHP 抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何检查我的 Apache 安装中是否启用了 PDO 支持? 下一篇:如何使用 PHPUnit 获得 100% 的代码覆盖率

                相关文章

              • <small id='UJV0g'></small><noframes id='UJV0g'>

                <i id='UJV0g'><tr id='UJV0g'><dt id='UJV0g'><q id='UJV0g'><span id='UJV0g'><b id='UJV0g'><form id='UJV0g'><ins id='UJV0g'></ins><ul id='UJV0g'></ul><sub id='UJV0g'></sub></form><legend id='UJV0g'></legend><bdo id='UJV0g'><pre id='UJV0g'><center id='UJV0g'></center></pre></bdo></b><th id='UJV0g'></th></span></q></dt></tr></i><div id='UJV0g'><tfoot id='UJV0g'></tfoot><dl id='UJV0g'><fieldset id='UJV0g'></fieldset></dl></div>

                1. <tfoot id='UJV0g'></tfoot>

                    <bdo id='UJV0g'></bdo><ul id='UJV0g'></ul>
                  <legend id='UJV0g'><style id='UJV0g'><dir id='UJV0g'><q id='UJV0g'></q></dir></style></legend>