Posted by: phillipnb | October 1, 2011

Abstract Class and PHP


Just like the word ‘abstract’, the explanation and use of abstract classes in PHP is also abstract. Different people have different opinion and understanding about abstract classes. In this post you will read about some general characteristics of abstract classes that are supported by a few examples.

The first explanation about abstract class is that ‘just by declaring a class using the keyword abstract will not make it an abstract class’. for e.g.

abstract class A {
    public function A() {
    }
}
class B extends A {}

$b = new B;

The above code will not throw an error even though it is not an abstract class.

Consider another example:

class A {
    abstract function test1();
    public function __construct() {
        echo "\n from abstract class A";
    }
    public function test2() {
        echo "\n hello";
    }
}

class B extends A {}

$b = new B;

In this example, PHP will throw an error saying that “class A contains 1 abstract method and therefore be declared as abstract or implement the remaining methods”. So, the definition of abstract class is that, if any class that has at least one abstract method then that class should be declared as abstract.

Let us discuss more characteristics of abstract classes. According to php manual, in php 5:

  • classes that are defined as abstract may not be instantiated
  • any class that contains at least one abstract method must also be abstract

In addition, abstract classes has also got the following characteristics:

  • methods that are declared as abstract will not contain method body or method implementation. It can have only the method signature
  • when inheriting from an abstract class the child class must implement all the methods named as abstract in the parent class
  • when inheriting from an abstract class the abstract methods implemented in the child class should be of the same or lesser visibility
  • abstract classes may have non abstract methods (which has a method body)
  • you cannot implement an abstract class, you can only extend it

So these are the written and unwritten rules about abstract classes. Let us take a look at another best practice that need to be observed while using an abstract class. Never declare an abstract class as ‘final’ because the keyword ‘final’ will make the class non-inheritable. This is against the purpose of abstract classes. We want the abstract class to be extendable.

We said that when implementing an abstract method of an abstract class the visibility should be same or less. What if we declare an abstract method as ‘private’?. Going by the rules of abstract classes when we implement a parent abstract method in the child class the visibility should be private(less) or protected/public(lesser visibility). But based on the rules of object oriented programming if we declare a method as private then it cannot be inherited because it will not be visible in the child class. So, we do not declare a method as private in an abstract class.

If we declare a class as abstract it prevents you from instantiating that class. That does not mean that we cannot access a static method declared in that class. for e.g.

abstract class A {
    static public function B() {
        echo "\n from class A, B()";
    }
}

In the above example, we can still access the static method by A::B().

So,in reality, abstract class is kind of an half baked pie. It is kind of a foundation on top of which a concrete class will be built. In general, if you are the only user of your classes, it is good to use abstract class especially if you have a class that already has some methods(signatures) which you want to implement in your new child class. You should also go for an abstract class if you think your base class is going to evolve with time. This means you can add new methods to the abstract class without breaking other classes that extends/inherits from this base abstract class.

At this stage another question that can trouble you will be “Can an abstract class extend another abstract class?”. The answer to that question is “yes, we can”. But the only catch is that an abstract class extending another abstract class should not implement the abstract methods from the parent class. If you do so, php will throw an error.

I think I have been able to give you a simple outline about abstract classes in PHP. So, keep working on abstract classes and till next time it is Happy PHPing.

About these ads

Responses

  1. very useful for beginner


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: