Skip to content

Instantly share code, notes, and snippets.

@rizkytegar
Created February 24, 2024 17:38
Show Gist options
  • Save rizkytegar/43a83ac0932d9e38ac9cfad65d17c06c to your computer and use it in GitHub Desktop.
Save rizkytegar/43a83ac0932d9e38ac9cfad65d17c06c to your computer and use it in GitHub Desktop.
<?php
// Definisikan kelas 'Person'
class Mahasiswa {
// Properties atau atribut
public $name;
public $nim;
// Constructor untuk menginisialisasi objek
public function __construct($name, $nim) {
$this->name = $name;
$this->nim = $nim;
}
// Method untuk menampilkan informasi tentang objek Person
public function introduce() {
// mengembalikan nilai
return "Nama: " . $this->name . ", Nim: " . $this->nim;
}
}
// Membuat objek dari kelas 'Person'
$mahasiswa1 = new Mahasiswa("Dwi", 30);
$mahasiswa2 = new Mahasiswa("Wahyu", 25);
// Memanggil method pada objek
echo $mahasiswa1->introduce();
echo "\n";
echo $mahasiswa2->introduce();
echo "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment