父 class 中的私有 属性 可以通过 PHP 中子 class 的实例访问和重新分配,但受保护的不能

Private property in parent class can be accessed and re-assigned through the instance of child class in PHP but protected one can't

我很疑惑为什么$produk1->harga = 500;还能改 (或重新分配值 500)private $harga 属性 尽管 private $harga in class Produk 具有 PRIVATE 可见性 ? $product1class Komik.

的实例

$produk1 = new Komik("Naruto", "Masashi Kishimoto", "Shonen Jump", 30000, 100);

然后 echo $produk1->harga; 它打印出 500 而不是 ERROR。但是当我将可见性更改为 protected $harga 时,它会打印出 ERROR。怎么会?我不明白。谢谢你的回答。

    <?php
        
        class Produk {
            public $judul = "judul",
                    $penulis = "penulis",
                    $penerbit = "penerbit";
            private $harga;
        
        
            public function __construct($judul, $penulis, $penerbit, $harga) {
                $this->judul = $judul;
                $this->penulis = $penulis;
                $this->penerbit = $penerbit;
                $this->harga = $harga;
            }
        
            public function detailInfo() {
                $str = "{$this->judul} | {$this->penulis}, {$this->penerbit} (Rp.{$this->harga})";
        
                return $str;
            }
        
        }
        
        class Komik extends Produk {
            public $jumlahHalaman = 0;
        
            public function __construct($judul, $penulis, $penerbit, $harga, $jumlahHalaman) {
        
                parent::__construct($judul, $penulis, $penerbit, $harga);
                $this->jumlahHalaman = $jumlahHalaman;
            }
        
            public function detailInfo() {
                $str = "Komik : " . parent::detailInfo() . " - {$this->jumlahHalaman} halaman.";
        
                return $str;
            }
        }
        
        
        class Game extends Produk {
        
            public $jumlahDurasi = 0;
        
            public function __construct($judul, $penulis, $penerbit, $harga, $jumlahDurasi) {
                
                parent::__construct($judul, $penulis, $penerbit, $harga);
                $this->jumlahDurasi = $jumlahDurasi;
            }
        
            public function detailInfo() {
                $str = "Game : " . parent::detailInfo() . " ~ {$this->jumlahDurasi} jam.";
        
                return $str;
            }
        }
        
        
        $produk1 = new Komik("Naruto", "Masashi Kishimoto", "Shonen Jump", 30000, 100);
        $produk2 = new Game("Uncharted", "Neil Druckmann", "Sony Computer", 250000, 5);
        
        
        echo $produk1->detailInfo();
        echo "<br>";
        echo $produk2->detailInfo();
        
        echo "<hr>";
        $produk1->harga = 500;
        echo $produk1->harga;

输出 =

漫画 : 火影忍者 | Masashi Kishimoto,Shonen Jump (Rp.30000) - 100 halaman。 游戏:神秘海域 | Neil Druckmann,Sony Computer (Rp.250000) ~ 5 jam.


500

IF $harga 属性 可见性受到保护

    <?php
        
        class Produk {
            public $judul = "judul",
                    $penulis = "penulis",
                    $penerbit = "penerbit";
            protected $harga;
.
.
.

输出 =

致命错误:未捕获错误:无法访问受保护的属性 Komik::$harga in ...

错误:无法访问受保护的属性 Komik::$harga ...

这是因为私有属性不被继承,当您试图从 child class - PHP 访问您的私有 属性 时找不到它并且动态创建新的 public 一个。请参见下面的示例。 受保护的属性是继承的,这就是为什么您不能从代码的其他部分访问它们的原因。

<?php

class A {
    private $property = 100;
    
    public function print() {
        echo $this->property;
    }
}

class B extends A {}

$b = new B();
$b->property = 500;
$b->print(); // 100

var_dump($b);
/* object(B)#1 (2) {
  ["property":"A":private]=>
  int(100)
  ["property"]=>
  int(500)
} */

可以在子 class 内部访问受保护的变量。但是,您不是在子 class 内部访问它,而是在子 class.

外部访问它

您可以通过像

这样的getter()函数来访问受保护的成员
public function getHarga()
{
    return $this->$harga;
}

您可以看到 doc 可见性。