Hi everyone, im trying to print getAktiva() but it return an error:
"Fatal error: Call to protected method indukPerusahaanABC::getAktiva() from context '' in C:\xampp\htdocs\sad.php on line 41"

Thanks in advance

<?php
    class indukPerusahaanABC{
        protected $nama;
        protected $jenis;
        protected $alamat;
        private $aktivaLancar;
        private $aktivaTetap;

        public function __construct($nama, $jenis, $alamat){
            $this->nama = $nama;
            $this->jenis = $jenis;
            $this->alamat = $alamat;
            $this->aktivaLancar = 35000000000;
            $this->aktivaTetap = 55000000000;
        }

        public function getNama(){
            return $this->nama;
        }

        public function getJenis(){
            return $this->jenis;
        }

        public function getAlamat(){
            return $this->alamat;
        }

        protected function getAktiva(){
            return $this->aktivaLancar+$this->aktivaTetap;
        }
    }




    $induk = new indukPerusahaanABC('CBA', 'Kredit', 'Jakarta Pusat');
    echo "Perusahaan Induk: ".$induk->getNama()."<br/>";
    echo "Bidang Area/Pekerjaan: ".$induk->getJenis()."<br/>";
    echo "Alamat Perusahaan Induk: ".$induk->getAlamat()."<br/>";
    echo "Aktiva Perusahaam Induk: Rp ".$induk->getAktiva()."<br/>";
?>

Recommended Answers

If you want to use this function outside the object make it public.

public function getAktiva(){
    return $this->aktivaLancar+$this->aktivaTetap;
}

This is usually the purpose of the get functions (getters): to get the data stored in protected or private variables.

Jump to Post

All 2 Replies

I think this may be the reason for your error:

Declaration

protected function getAktiva()

essentially this condition means that this proteted class can only be accessed within the originating class (class indukPerusahaanABC) and within all of its eventual subclasses.

If you want to use this function outside the object make it public.

public function getAktiva(){
    return $this->aktivaLancar+$this->aktivaTetap;
}

This is usually the purpose of the get functions (getters): to get the data stored in protected or private variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.