有關php類的private屬性繼承問題詳解

來源:互聯網
上載者:User
  1. class employee{

  2. private $sal=3000;
  3. //protected $sal=3000;
  4. public function getSal(){
  5. return $this->sal;
  6. }
  7. }
  8. class Manager extends employee {
  9. protected $sal=5000;

  10. public function getParentSal(){

  11. //這裡返回的是父類的private屬性.
  12. return parent::getSal();
  13. }
  14. }
  15. $manager = new Manager();
  16. echo "PHP ".phpversion()."
    ";
  17. echo $manager->getSal();
  18. echo "
    ";
  19. echo "parent's \$sal ".$manager->getParentSal();
  20. ?>

複製代碼

程式運行結果:PHP 5.3.83000parent's $sal 3000

如果父類中的屬性被子類重寫了。結果是這樣的。注意 第5行的屬性定義變成了protected。

  1. class employee{

  2. //private $sal=3000;
  3. protected $sal=3000;
  4. public function getSal(){
  5. return $this->sal;
  6. }
  7. }

  8. class Manager extends employee {

  9. protected $sal=5000;

  10. public function getParentSal(){

  11. //這裡返回的是父類的private屬性.
  12. return parent::getSal();
  13. }
  14. }
  15. $manager = new Manager();
  16. echo "PHP ".phpversion()."
    ";
  17. echo $manager->getSal();
  18. echo "
    ";
  19. echo "parent's \$sal ".$manager->getParentSal();
  20. ?>

複製代碼

程式運行結果:PHP 5.3.85000parent's $sal 5000

第一個列子中 父類的private $sal沒有被重寫 所以$manager->getSal()這個父類的方法 調用的是父類自己的私人屬性$sal 此時記憶體中有兩個$sal第二個列子中 父類的protected $sal被重寫 $manager->getSal()這個父類的方法 調用已經被重寫的$sal 父類的$sal在記憶體中是不存在的 此時記憶體中只有一個$sal接下來看第三個列子子類中重寫的方法對當前private有效。

  1. class employee{

  2. private $sal=3000;
  3. public function getSal(){
  4. return $this->sal;
  5. }
  6. }

  7. class Manager extends employee {

  8. private $sal=5000;
  9. //重寫過的方法
  10. public function getSal(){
  11. return $this->sal;
  12. }
  13. public function getParentSal(){
  14. //這裡返回的是父類的private屬性.
  15. return parent::getSal();
  16. }
  17. }
  18. $manager = new Manager();
  19. echo "PHP ".phpversion()."
    ";
  20. echo $manager->getSal();
  21. echo "
    ";
  22. echo "parent's \$sal ".$manager->getParentSal();
  23. ?>

複製代碼

運行結果PHP 5.3.85000parent's $sal 3000

這個列子中子類重寫getSal()方法 所以他調用的是子類的屬性如果你注釋子類的這一行//private $sal=5000;你會發現一個錯誤:Notice: Undefined property: Manager::$sal in E:\wamp\www\oo\2-5\2-5-3.php on line 14如果注釋掉12行的子類重寫方法 那麼echo $manager->getSal();得到的結果是 父類的私人屬性$sal 3000

開啟zend調試狀態看看,記憶體中的情況。注意最下面,有兩個$sal 。分別是 3000 和 5000 。

  1. class employee{
  2. private $sal=3000;
  3. public function getSal(){
  4. return $this->sal;
  5. }
  6. }
  7. class Manager extends employee {
  8. protected $sal=5000;
  9. public function getParentSal(){
  10. return $this->sal;
  11. }
  12. }
  13. $manager = new Manager();
  14. echo "PHP ".phpversion()."
    ";
  15. echo $manager->getSal();
  16. ?>
複製代碼

程式運行結果:PHP 5.3.83000

將父類的屬性$sal 改成 protected ,子類重寫了父類的屬性。在記憶體中只有一個 $sal 。

  1. class employee{
  2. protected $sal=3000;
  3. public function getSal(){
  4. return $this->sal;
  5. }
  6. }
  7. class Manager extends employee {
  8. protected $sal=5000;
  9. public function getParentSal(){
  10. return $this->sal;
  11. }
  12. }
  13. $manager = new Manager();
  14. echo "PHP ".phpversion()."
    ";
  15. echo $manager->getSal();
  16. ?>
複製代碼

程式運行結果:PHP 5.3.85000注意:PHP5調用父類用的是parent:: 而不是 parent-> ,這足以說明PHP5不想在記憶體中讓父類也被建立。PHP5想讓繼承變的比Java更簡單。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.