Junior — Middle
What types of methods are allowed when declared inside an abstract class?
sobes.tech AI
Answer from AI
In an abstract class in PHP, the following types of methods are allowed:
-
Abstract methods — declared without implementation and must be implemented in child classes. Declared with the
abstractkeyword. -
Regular methods with implementation — can contain specific code that is inherited by child classes.
-
Static methods — methods that can be called without creating an instance of the class.
-
Methods with different visibility scopes —
public,protected,private.
Example:
abstract class Animal {
abstract public function makeSound(); // abstract method
public function sleep() {
echo "Sleeps...";
}
protected static function helper() {
// static protected method
}
}
Thus, an abstract class can contain both abstract methods and methods with implementation, allowing to define a common interface and partially implement functionality.