Sobes.tech
Junior — Middle

Millised meetoditüübid on lubatud abstraktse klassi sees deklaratsioonil?

sobes.tech AI

Vastus AI-lt

In an abstract class in PHP, the following types of methods are allowed:

  • Abstract methods — declared without implementation and must be implemented in derived classes. Declared with the keyword abstract.

  • Regular methods with implementation — can contain concrete code, which is inherited by derived classes.

  • Static methods — methods that can be called without creating an instance of the class.

  • Methods with different visibility levelspublic, protected, private.

Example:

abstract class Animal {
    abstract public function makeSound(); // abstract method

    public function sleep() {
        echo "Sleeping...";
    }

    protected static function helper() {
        // protected static method
    }
}

Thus, an abstract class can contain both abstract methods and methods with implementation, allowing to define a common interface and partially implement functionality.