How to work with abstract PHP?

Why are you using such an abstract? Does work accelerate or what exactly is it?

// 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 for extending abstraction here? It bothers me. What can I get from him now?

+4
oop php zend-framework
Aug 30 '11 at 12:31 on
source share
3 answers

You can use abstract classes to define and partially perform common tasks that an extended class should perform. As an explanation of this is difficult without an example, consider this:

Without abstract classes, you will need to define two base classes with the same methods and implementation. Since OOP works to prevent code duplication, this is completely 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 } } 

Now you have some common properties and methods that are duplicated in two classes. To avoid this, we could define an abstract class from which Car and Truck extend. Thus, common functions are stored in one place, and extended classes will implement specific properties and methods for a truck or car.

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

In this way, you guarantee that at least every class that extends Vehicle must have the specified brand, and that the general 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.

+3
Aug 30 2018-11-12T00:
source share

This will not be a popular answer, but still ...

abstract , interface , private and other keywords borrowed from Java are elements of cult cult programming and do not serve a real purpose in PHP, except to make the author more “serious” and “professional”.

Explanation: these keywords are compile-time contracts that do not affect how your program works, and are intended only to help the compiler ... if you have them. In a compiled language, such as Java or C #, you physically cannot deploy a program that violates a contract, for example. does not implement the abstract method. You just do not compile it. This is a good thing, because you can fix some kinds of errors very quickly, without testing or debugging.

PHP, by contrast, does not have a compiler and performs all contract checks at runtime. This is Bad Thing because you need to test and debug to find contract violations manually. Consider the following:

 class Abs { abstract function implementMe(); } if ($_GET['x'] == 'foo') include "GoodClass.php"; if ($_GET['x'] == 'bar') include "BadClass.php"; 

where "BadClass" extends "Abs" but does not implement the "implementMe" method. This script can be deployed and will work fine until someone calls it "? X = bar" and then bang! - your program suddenly crashes. To make matters worse, this will be a “fatal” mistake, so you won’t even be able to handle it in a reasonable way.

That is, abstract and friends are not only useless, but also very harmful to PHP. Not only do they not help you find errors, but they are also a potential source of even more failures. Stay away.

+3
Aug 30 2018-11-11T00:
source share

Thus, you can implement various search adapters, and all of them must implement this search method. See inheritance here .
Basically, you get that each class extending "Search_Adapter_Abstract" can be used as a search adapter. The behavior changes (because the method is searched differently), but you guarantee that the “search” method exists with this signature.

+2
Aug 30 '11 at 12:33
source share



All Articles