Contoh Class / OOP di PHP 5

Discussion in 'Programming' started by apartemen, Sep 27, 2014.

  1. apartemen

    apartemen New Member

    Joined:
    Sep 25, 2014
    Messages:
    12
    Likes Received:
    2
    Trophy Points:
    3
    Berikut contoh OOP (class) di PHP 5, berisi demo sifat inheritance (pewarisan), dan contoh dasar class di PHP 5 :)

    PHP:
    <?php
    class MakhlukHidup {
        private 
    $jenis;

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

        public function 
    bernafas() {
            echo 
    '<p>Menghirup udara.</p>';
            return 
    $this;
        }

        public function 
    jenis() {
            echo 
    '<p>Jenis makhluk hidup = '.$this->jenis.'.</p>';
            return 
    $this;
        }

        public function 
    makan() {
            echo 
    '<p>Makan makanan MENTAH.</p>';
            return 
    $this;
        }
    }

    class 
    Manusia extends MakhlukHidup {
        private 
    $nama;

        public function 
    __construct($nama) {
            
    parent::__construct('Homo Sapiens');
            
    $this->nama $nama;
        }

        public function 
    makan() {
            echo 
    '<p>Makan makanan MATANG.</p>';
            return 
    $this;
        }

        public function 
    nama() {
            echo 
    '<p>Nama = '.$this->nama.'.</p>';
            return 
    $this;
        }
      
        public function 
    tidur() {
            echo 
    '<p>Tidur di springbed.</p>';
            return 
    $this;
        }
    }

    // Objek makhluk hidup
    $jerry = new MakhlukHidup('Tikus');
    $jerry->jenis()->bernafas()->makan();

    echo 
    '<hr />';

    // Objek manusia
    $bambang = new Manusia('Bambang');
    $bambang->jenis()->bernafas()->makan()->nama()->tidur();
    ?>
     
    Priana Saputra likes this.
  2. Ardilas

    Ardilas Super Level

    Joined:
    Feb 18, 2013
    Messages:
    4,243
    Likes Received:
    317
    Trophy Points:
    83
    Google+:
    Saya kurang paham nih. Hasilnya bagaimana?
     
  3. apartemen

    apartemen New Member

    Joined:
    Sep 25, 2014
    Messages:
    12
    Likes Received:
    2
    Trophy Points:
    3
    Hasilnya : :)
    -------------------------
    Jenis makhluk hidup = Tikus.

    Menghirup udara.

    Makan makanan MENTAH.
    --------------------------------
    Jenis makhluk hidup = Homo Sapiens.

    Menghirup udara.

    Makan makanan MATANG.

    Nama = Bambang.

    Tidur di springbed.
     
  4. Grant Verleend

    Grant Verleend Active Member

    Joined:
    Sep 10, 2014
    Messages:
    1,234
    Likes Received:
    35
    Trophy Points:
    48
    Google+:
    sip, bang nanbah ilmu nie
     
  5. Priana Saputra

    Priana Saputra Member

    Joined:
    May 20, 2014
    Messages:
    123
    Likes Received:
    7
    Trophy Points:
    18
    Google+:
    Sip Gan...! Materi baru nih, tp sekarang baru belajar Header Location dulu..! masih belum mudeng, apa ada tutorial bagus untuk materi yang saya maksud...?:D
     
  6. apartemen

    apartemen New Member

    Joined:
    Sep 25, 2014
    Messages:
    12
    Likes Received:
    2
    Trophy Points:
    3
    Header location untuk redirect pengunjung ke alamat lain ya? :)
    PHP:
    <?php
    header
    ('Location: http://www.facebook.com');
    exit();
    ?>
     
  7. Batik Trusmi Online

    Batik Trusmi Online Member

    Joined:
    Aug 12, 2014
    Messages:
    88
    Likes Received:
    4
    Trophy Points:
    18
    kebetulan saya lagi belajar oop, di sini nongol materi oop.
     
  8. Devita

    Devita Active Member

    Joined:
    Oct 11, 2014
    Messages:
    1,196
    Likes Received:
    43
    Trophy Points:
    48
    Google+:
    Belajar oop bikin penasaran pastinya.
     
  9. tonbad

    tonbad Member

    Joined:
    Dec 21, 2014
    Messages:
    260
    Likes Received:
    24
    Trophy Points:
    18
    Google+:
    oke gan... thanks sharingnya... jadi inget programming java dulu... mesti OOP... hehe
     
  10. oboth

    oboth New Member

    Joined:
    Feb 8, 2015
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    6
    Mantap thanks ilmu nya baru belajar OOP PHP nih
     
  11. Wedepe

    Wedepe New Member

    Joined:
    Mar 18, 2015
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    6
    Google+:
    Ini

    $this->jenis = $jenis;

    maksudnya apa ya gan?
     
Loading...

Share This Page