Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Khởi tạo lớp con (Instance child class) trong phương thức static abstract PHP

Dùng $anyObjectName = new static() cùng với self.

Ví dụ

<!DOCTYPE html> <html> <body> <?php 
abstract class Base {
    protected static $fullName = '';
    abstract protected function customFunction();
    public static function get_obj($param1 = null, $param2 = null) {
        $obj = new static();
        $obj->customFunction();
    }
    public static function getFullName() {
        return static::$fullName;
    }
}
class Child extends Base {
    protected static $fullName = 'John Doe';
    protected function customFunction() {
        echo self::getFullName() . "
"; echo $this; } } Child::get_obj(); ?> </body> </html>

Kết quả

John Doe