Why do we use abstract class in php?
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).
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. Hope that helps. PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature; they cannot define the implementation. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child class, and follow the usual inheritance and signature compatibility rules. Example #1 Abstract class example
ConcreteClass1 extends AbstractClass The above example will output: ConcreteClass1 FOO_ConcreteClass1 ConcreteClass2 FOO_ConcreteClass2 Example #2 Abstract class example
ConcreteClass extends AbstractClass
The above example will output: ironiridis at gmail dot com ¶ 14 years ago
mbajoras at gmail dot com ¶ 12 years ago
eat() {
a dot tsiaparas at watergate dot gr ¶ 11 years ago
joelhy ¶ 11 years ago
jai at shaped dot ca ¶ 5 years ago
defaultProductImplementation extends defaultProductAbstraction { shaman_master at list dot ru ¶ 3 years ago
Adapter shewa12kpi at gmail dot com ¶ 1 year ago
__construct($fn, $ln){ swashata4u at gmail dot com ¶ 4 years ago
Base implements Element {/** sneakyimp at hotmail dot com ¶ 14 years ago
jai at shaped dot ca ¶ 5 years ago
myDB implements dbInterface { bishop ¶ 12 years ago
Bar extends Foo { Eugeny at Kostanay dot KZ ¶ 5 years ago
someChildClass extends anotherAbsClass Malcolm ¶ 6 years ago
joebert ¶ 15 years ago
arma99eDAN at yahoo dot com ¶ 7 years ago
pete at surfaceeffect dot com ¶ 12 years ago
designbyjeeba at gmail dot com ¶ 11 years ago
nathan dot vorbei dot tech at gmail dot com ¶ 12 years ago
ClassTwo extends ClassOne { Cheese Doodle ¶ 12 years ago
eeescalona ¶ 14 years ago
write_info(); aloydev2586 at gmail dot com ¶ 7 years ago
rmoisto at gmail dot com ¶ 6 years ago
work(); sam at righthandedmonkey dot com ¶ 8 years ago
cart extends horse { nikola at petkanski dot com ¶ 8 years ago
oliver at ananit dot de ¶ 10 years ago
ProductModel extends AbstractModel balbuf ¶ 6 years ago
Why do we use an abstract class?An abstract class is used if you want to provide a common, implemented functionality among all the implementations of the component. Abstract classes will allow you to partially implement your class, whereas interfaces would have no implementation for any members whatsoever.
What is the use of abstraction in PHP?Abstraction in PHP
Data Abstraction is the most important features of any OOPS programming language. It shows only useful information, remaining are hidden form the end user. Abstraction is the any representation of data in which the implementation details are hidden (abstracted).
|