Search code examples
phpoopabstract-classabstract-data-type

what is difference between abstract class and abstract function


I know that if I create a abstract class, then I can't create a instance of it, and it will be just a basic class (extending it for other classes). Now I want to know what is abstract function? (or also is there abstract property?)

I saw a function without definition in a abstract class (also the function was abstract), so why? Something like this:

Abstract class test{
      Abstract function index();
}

Solution

  • An abstract class cannot be instantiated. Let's say you have:

    Abstract class People {
    
    }
    

    You cannot do $people = new People();

    You need to extend it to be able to instantiate it, like:

    class Man extends People {
    
    }
    
    $people = new Man();
    

    Regarding Abstract methods, they only contain the method signature in the abstract class and they MUST be implemented in the children classes.

    Abstract class People {
      abstract public function getAge();
    }
    class Man extends People {
      public function getAge() {
        //Blah Blah
      }
    }