简体   繁体   中英

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();
}

An abstract function is a function that is not yet implemented. The implementation of the abstract function has to be done in inherited classes.

A class with an abstract function has to be an abstract class.

The abstract function allow you to write algorithms in the abstract class without defining all sub-functions (declared abstract) because these sub-functions may depend on the context of the concrete inherited classes.

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

From: http://php.net/manual/en/language.oop5.abstract.php

PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM