Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 694
Sreeraj Avatar asked Jun 16 '11 14:06

Sreeraj


People also ask

What is benefit of abstract class in PHP?

Unlike C++ abstract classes in PHP are declared with the help of abstract keyword. Use of abstract classes are that all base classes implementing this class should give implementation of abstract methods declared in parent class. An abstract class can contain abstract as well as non abstract methods.

What is the benefit of using abstract class?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Why do we use abstract class and interface in PHP?

Abstract classes let you provide some degree of implementation, interfaces are pure templates. An interface can only define functionality, it can never implement it. Any class that implements the interface commits to implementing all the methods it defines or it must be declared abstract.

Why abstract class is faster than interface?

4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because interface involves a search before calling any overridden method in Java.


1 Answers

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.

Hope that helps.

like image 125
Berry Langerak Avatar answered Oct 01 '22 03:10

Berry Langerak