PHP语言的构造方法提升(Promotion)

class C {
    public int $a;
    public int $b;

    public function __construct(int $a, int $b = 1) {
        $this->a = $a;
        $this->b = $b;
    }
}

//简写
class C {
    public function __construct(public int $a, public int $b = 1) {
    }
}
class C {
    protected int $a;
    protected int $b;

    public function __construct(int $a, int $b = 1) {
        $this->a = $a;
        $this->b = $b;
    }
}

//简写
class C {
    public function __construct(protected int $a, protected int $b = 1) {
    }
}
class C {
    private int $a;
    private int $b;

    public function __construct(int $a, int $b = 1) {
        $this->a = $a;
        $this->b = $b;
    }
}

//简写
class C {
    public function __construct(private int $a, private int $b = 1) {
    }
}

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/experience/phpexp/38304.html

huoxiaoqiang的头像huoxiaoqiang

发表回复

登录后才能评论
分享本页
返回顶部