What are the advantage if i use abstract class in php? [duplicate]
Possible Duplicate:
interface vs abstract class
What are the advantage if i use abstract class in php?
What is the aim if i use abstract class or interfaces?
Both are simply creating defenition names with out body
What are the advantage if i use abstract class in php? i cant find anything good on that. I think i can easily do all work with out using the abstract class?
You could, naturally. However, if there are many objects that are of pretty much the same type, it might help to extract common functionality out into a "base" class, which means you don't have to duplicate that logic.
There are actually two reasons though. The first reason, for me, would be that all descendants of your abstract class have the same type, and both adhere to the exact same interface. That means that a PDF document for example will have the same interface as a docx document, and the client code doesn't have to care which object it's handling. Short example (in PHP).
<?php
abstract class Document {
protected $author;
public function __construct( $author ) {
$this->author = $author;
}
abstract public function render( );
public function author( ) {
return $this->author;
}
}
class PdfDocument extends Document {
public function render( ) {
// do something PDF specific here.
}
}
class DocxDocument extends Document {
public function render( ) {
// do something DOCX specific here.
}
}
class DocumentHandler {
public function handle( Document $document ) {
$this->log( 'Author has been read ' . $document->author( ) );
return $document->render( );
}
}
First of all; mind the fact that the DocumentHandler class has no knowledge of which type of Document it's actually handling. It doesn't even care. It's ignorant. However, it does know which methods can be called upon, because the interface between the two types of Documents are the same. This is called polymorphism, and could just as easily be achieved with the implementation of a Document interface.
The second part is; if each and every document has an author, and that author is always requested, you could copy the method over to the PdfDocument as well as the DocxDocument, but you'd be duplicating yourself. For instance, if you decide that you want the author to written with capitals, and you'd change return $this->author to ucwords( $this->author ), you'd have to do it as many times as you've copied that method. Using an abstract class, you can define behaviour, while marking the class itself as incomplete. This comes in very handy.
Abstract classes help you when you have many classes that have the same methods.
Example:
abstract class Foo {
public function foo1() {
//Do something
}
public abstract class foo2();
}
class Bar extends Foo {
public class foo2() {
//Do something
}
}
class Baz extends Foo {
}
What will happen:
- You can't use
new Foo();
,Foo
is abstract. - You will be able to use
Bar.foo1()
andBaz.foo1()
, they will do the same thing. - You will have an error, because
Baz
doesn't implement the abstact methodfoo2
.
Example where it is usefull:
abstract class Shape {
public function display() { /* ... */ }
//...
}
class Circle extends Shape {
//...
}
class Rectangle extends Shape {
//...
}
//...
You want every class to be able to display()
, but there is no such thing as "Shape" by itself.
Not all of the abstract class' methods must be empty, there may be some basic methods (and properties) to work with. For example - you have and e-shop and you develop an abstract class to import products. The class has a metod to save products to db, to generate product's url and an abstract method to retrieve products from somewhere (which therefore has to be implemented in the extended class). Interface only has blank methods and no properties (can have constants though), so tere may be no actual logic, just method constants, method names and their access modifiers.
As the name suggests, the purpose of an interface is to explicitly declare the interface to data and methods provided by a class and the instances, without the need to code those methods right away. The classic example is that of geometric shapes. Such an interface could define a method that produces the area of such a shape:
interface Shape {
public function getArea();
}
There could be a number of different classes implementing this interface like Circle
and Square
that would provide different implementations for the method getArea()
. You could then implement a function that displays information on any geometric shape:
function displayInformation(Shape $shape) {
echo "The area of this shape is: " . $shape->getArea();
}
You could pass any object implemented the Shape
interface to this function, and the interface guarantees that the getArea()
method is present.
Of course, these concepts might be more useful in programming languages that are more strongly typed than PHP.
精彩评论