Tutorial

Abstract in PHP

For Abstact keyword we can say that, abstract is type of the class and class which we can’t create a object of it. Surprised????

Yes, you have read it correctly. We can not create object of the abstract class using [code]new[/code] keyword. Also we can not call the abstract methods directly as we can’t create a object of that class.

Abstract class can be used some what like an interface in PHP. So basically we can implement class using abstract. We can’t extend more than one abstract class while we can implement more than one interface.

Basic Abstract Class

[cc lang=”php”]
abstract class vehicle()
{

}

$new_vehicle = new vehicle();
// This will give following error:
// Fatal error: Cannot instantiate abstract class

[/cc]

Declaring Abstract Method

Abstract method can be declared using abstract keyword. abstract method must not contain implementation part. We can just declare it.

[cc lang=”php”]
abstract class vehicle()
{
abstract function set_name() {}
}

// This will end up with below Error:
// Fatal error:
// Abstract function vehicle::set_name() cannot contain body

[/cc]

Proper way to Declare Static Method

[cc lang=”php”]
abstract class vehicle()
{
abstract function set_name();
}

[/cc]

Thing to Keep in Mind

Class which contain the abstract method must be declared as a abstract. Have a look at below code:

[cc lang=”php”]
class vehicle()
{
abstract function set_name()
}

// This will end up with below Error:
// Fatal error:
// Class vehicle contains 1 abstract method and must
// therefore be declared abstract or
// implement the remaining methods

[/cc]

Example with abstract Class and Method

[cc lang=”php”]

abstract class vehicle()
{
abstract function set_name();
}

[/cc]

Note: Class which contain the abstract method must be declared as a abstract.

Extending an Abstract Class

We have to extend an abstract class to use the methods of that class. To call the abstract method you must extend the class which contain that abstract method and implementation code can be written in child class.

Child class must have to implement all abstract methods of the parent class. And the access level of the methods of the child class must be same or lower.

So if abstract method is declared as public in abstract class then you can’t implement it as a protected or private in child class.

[cc lang=”php”]

abstract class vehicle
{
abstract function set_name();
}

class bike extends vehicle
{
function set_name()
{
echo “Yamaha”;
}
}

$obj_bike = new bike();

$obj_bike->set_name();

// Output : Yamaha
[/cc]

Note: Child class must have to implement all abstract methods of the parent class.

Conclusion:

You can work with abstract classes in PHP some what like an Interface.

Shares:
  • Marco
    Marco
    October 29, 2011 at 5:45 am

    Greate post!

    When the interface?

    :)

    Reply
  • Interface in PHP | Expert PHP Developer
    October 31, 2011 at 12:11 am

    […] some what like as abstract class, you can find more about the abstract class from this article on Abstract in PHP.Interface is just like a coding standard because interface allow us to create a code/class which […]

    Reply
  • auchomage
    auchomage
    March 10, 2013 at 4:10 am

    Very useful.
    I am just learning about this, and got stuck because in the example I was following from a book, I missed the fact that the parent class MUST be declared as an ABSTRACT CLASS. This article caused me to go back and pick up the error I made. Thank you very much.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *