2010-01-04 83 views
0

我有一個抽象的父類protected $instanceVariable。現在有一個ChildClass它也有一個private $instanceVariable。它設置一個初始值,如:通過繼承重新聲明或覆蓋實例變量是否有效?

private $instanceVariable = 5; 

所以子類覆蓋它,並改變能見度私人。父類不會爲該實例變量賦值。它也在父類中聲明,因爲有方法可以訪問它。

這對PHP來說很好嗎?

回答

1

你打破Liskov Substitution Principle - 我應該能夠從ChildClass安全中繼承,因爲我的GrandChild類可以像對待ParentClass一樣處理ChildClass。那就是GrandChild應該能夠訪問受保護的$ instanceVariable。

1

PHP不會讓你聲明這樣一個成員,你會得到這樣的錯誤:

Access level to d::$x must be protected (as in class c) or weaker