Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

第一种写法

class test{
    public $a;
    protected $b;
    private $c;
    
    public function __construct($a, $b,$c)
    {
        $this->a = $a;
        $this->b = $b;
        $this->c = $c;
    }
}

第二种写法

class test{
    public function __construct($a, $b,$c)
    {
        $this->a = $a;
        $this->b = $b;
        $this->c = $c;
    }
}

请问这两种写法有什么区别吗还是没有区别,能想到的就是第二种a,b,c三个属性都是为public


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

有区别的
1.执行顺序不一样,类属性初始化调用优先级高于构造函数
2.初始化可以自定义修饰符


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...