Why should we use abstract class in php?

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].



Then php will show this fatal message:
Fatal error: Declaration of ConcreteClass::prefixName[] must be compatible with AbstractClass::prefixName[$name] in /index.php on line 23

Stange enough it gives an incorrect declaration of "ConcreteClass::prefixName[]"... It is missing both arguments. Because of that I'm assuming that this is just a bug that maybe already has been taking care of in newer versions. [Or is just specific to my version] I'm mainly noting this because it was driving me absolutely insane in some test code that I was writing derived from Example #2 [without a default value for an extra argument]. Perhaps this saves some frustrations to other people.

--
Please note that i'm running this on php5.5.
OS: ubuntu-16.04-server-amd64.iso
Repo: ppa:ondrej/php

# php5.5 --version
PHP 5.5.36-2+donate.sury.org~xenial+1 [cli]
Copyright [c] 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright [c] 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright [c] 1999-2015, by Zend Technologies

joebert

15 years ago

I don't agree with jfkallens' last comparison between Abstract Classes & Object Interfaces completely.

In an Abstract Class, you can define how some methods work, where as in an Object Interface you can not.

An Object Interface is essentually nothing but a list of function names that a class must define if the class implements that interface.

An Abstract Class is essentually a prototype which hints towards what extending classes should be doing.
An Abstract Class can also be thought of as a Base Class that provides some basic functionality, & also defines a built-in Object Interface that all extending classes will implement.

So, an Object Interface is really a built-in part of an Abstract Class.

arma99eDAN at yahoo dot com

7 years ago

You can use an abstract class like this too:

abstract class A{
    public function show[]{
        echo 'A';
    }
}
class B extends A{
    public function hello[]{
        echo 'B';
        parent::show[];
    }
}

$obj = new B;
$obj->hello[]; // BA
# See that the abstract class does not have at least one abstract method
# Even in this case, I'm still able to extend it, or call its non-abstract member

pete at surfaceeffect dot com

12 years ago

One fairly important difference between php's abstract functions and, say, Java, is that php does not specify the return type in any way - or indeed whether there has to be one.



could be implemented by...



or



So you need to take care that incompatibilities don't arise due to not returning the right kind of value and this is not enforced in any way.

designbyjeeba at gmail dot com

11 years ago

Please be aware of the visibility of the parent fields. If the fields are private, then you are not going to see those fields in their childrens. Its basic OOP, but can be problematic sometimes.

nathan dot vorbei dot tech at gmail dot com

12 years ago

"additionally, these methods must be defined with the same [or a less restricted] visibility."

The words were not restricted in abstract class but also normal classes,
the method in child Class which overwrites the parent Class can also change the the visibility of the method to same or less restricted.
for example:

Chủ Đề