How to work with PHP abstract?(如何使用 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
}
}
现在你有一些通用的属性和方法,它们在两个类中重复.为了防止这种情况,我们可以定义一个抽象类,从它扩展 Car 和 Truck.这样,通用功能就保存在一个地方,扩展类将为卡车或汽车实现特定的属性和方法.
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 抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 PHP 抽象?
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
