PHP: call child constructor from static method in parent

前端 未结 4 1823
天命终不由人
天命终不由人 2021-01-17 17:45

I want to have a static method in a parent class that creates instances of whatever subclass i call this method on.

An example to make this more clear:

cl         


        
4条回答
  •  独厮守ぢ
    2021-01-17 18:10

    make the parent class an abstract class and make the parent method also an abstract

    abstract static class parent {
         abstract function make_method() {
             // your process
         }
    }
    
    class child extends parent {
         public function __construct() {
              parent::make_method();
         }
    }
    

提交回复
热议问题