0

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/>";
?>

Edited by mantapgan

3
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by broj1
1

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.

Edited by jajarvin

2

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.

Edited by broj1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.