Abstract in PHP

By Avinash

October 27, 2011Tutorial6 Comments

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 new 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

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

Declaring Abstract Method

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

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

Proper way to Declare Static Method

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

Thing to Keep in Mind

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

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

Example with abstract Class and Method

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

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.

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

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.

Share This Article

  • Marco

    Greate post!

    When the interface?

    :)

  • Pingback: Interface in PHP | Expert PHP Developer

  • auchomage

    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.