PBOPHP
PBOPHP
<?php
class book
{
// properti
public $title = "Mastering PHP ";
public $author = "Ismail";
public $volume;
public $price;
// method
public function getTitle()
{
return $this->title . " Written by " . $this->author;
}
}
// objek
$product1 = new book();
// menampilkan class
print $product1->getTitle();
contoh2
<?php
class book
{
public $title = "Mastering PHP ";
public $author = "ISMAIL";
public $volume;
public $price;
Contoh 3
<?php
/**
* 1. membuat sebuah class
*/
class Mobil {
}
/**
* 2. menambah property kedalam class
* catatan : property juga bisa disebut dengan variable
*/
class Robot {
public $nama;
public $negara = 'Jepang'; /** jepang dalam hal ini adalah value */
}
/**
* 3. membuat objek dari sebuah class
* dalam hal ini class-nya adalah superhero
*/
class SuperHero {
/**
* 4. Mengakses Property
*/
class Penjahat {
/**
* 5. Mengatur nilai property
*/
class Makanan {
/**
* 6. Menambah method dan menjalankannya
* - menambah method di sini yang dimaksud adalah method dalam sebuah class
*/
class Belajar {
/**
* 7. Membuat beberapa objek dari suatu class
*/
class Komputer {
public $merk = '';
public $harga = '';
public $fungsi = '';
}
echo "Aku punya " . $komputer1->merk . " harganya " . $komputer1->harga . " ku
gunakan untuk " . $komputer1->fungsi;
Buatlah kelas manusia dengan property nama, alamat method berlari berjalan
Buatlah objek dari kelas manusia dengan nama amir dan ana