How to declare abstract method in non-abstract class in PHP?

后端 未结 7 500
不知归路
不知归路 2020-12-14 14:51
class absclass {
    abstract public function fuc();
}

reports:

PHP Fatal error: Class absclass contains 1 abstract metho

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 15:02

    An abstract class cannot be directly instantiated, but it can contain both abstract and non-abstract methods.

    If you extend an abstract class, you have to either implement all its abstract functions, or make the subclass abstract.

    You cannot override a regular method and make it abstract, but you must (eventually) override all abstract methods and make them non-abstract.

    name = $name;
            $this->gender = $gender;
        }
    
        public function getName() {return $this->name;}
        public function setName($name) {$this->name = $name;}
        public function getGender() {return $this->gender;}
        public function setGender($gender) {$this->gender = $gender;}
    
        abstract public function bark();
    
    }
    
    // non-abstract class inheritting from an abstract class - this one has to implement all inherited abstract methods.
    class Daschund extends Dog {
        public function bark() {
            print "bowowwaoar" . PHP_EOL;
        }
    }
    
    // this class causes a compilation error, because it fails to implement bark().
    class BadDog extends Dog {
        // boom!  where's bark() ?
    }
    
    // this one succeeds in compiling, 
    // it's passing the buck of implementing it's inheritted abstract methods on to sub classes.
    abstract class PassTheBuckDog extends Dog {
        // no boom. only non-abstract subclasses have to bark(). 
    }
    
    $dog = new Daschund('Fred', 'male');
    $dog->setGender('female');
    
    print "name: " . $dog->getName() . PHP_EOL;
    print "gender: ". $dog->getGender() . PHP_EOL;
    
    $dog->bark();
    
    ?>
    

    That program bombs with:

    PHP Fatal error: Class BadDog contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Dog::bark)

    If you comment out the BadDog class, then the output is:

    name: Fred
    gender: female
    bowowwaoar
    

    If you try to instantiate a Dog or a PassTheBuckDog directly, like this:

    $wrong = new Dog('somma','it');
    $bad = new PassTheBuckDog('phamous','monster');
    

    ..it bombs with:

    PHP Fatal error: Cannot instantiate abstract class Dog

    or (if you comment out the $wrong line)

    PHP Fatal error: Cannot instantiate abstract class PassTheBuckDog

    You can, however, call a static function of an abstract class:

    abstract class Dog {
        ..
        public static function getBarker($classname, $name, $gender) {
            return new $classname($name, $gender);
        }
        ..
    }
    
    ..
    
    $other_dog = Dog::getBarker('Daschund', 'Wilma', 'female');
    $other_dog->bark();
    

    That works just fine.

提交回复
热议问题