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:
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:
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
CarandTruckare 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.This way, you ensure that atleast every class that extends
Vehiclehas to have a brand specified and the commongasPerMile()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.