0% found this document useful (0 votes)
2 views2 pages

PHP OOP Concepts: Examples Explained

The document provides examples of PHP Object-Oriented Programming (OOP) concepts including encapsulation, inheritance, polymorphism, and abstraction. It includes a Database class for database connection, a Mahasiswa class demonstrating encapsulation, a User class with a Dosen subclass for inheritance, and a User class with an Admin subclass showcasing polymorphism. Additionally, it features an abstract class Laporan to illustrate abstraction in PHP.

Uploaded by

Laura Anggraini
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

PHP OOP Concepts: Examples Explained

The document provides examples of PHP Object-Oriented Programming (OOP) concepts including encapsulation, inheritance, polymorphism, and abstraction. It includes a Database class for database connection, a Mahasiswa class demonstrating encapsulation, a User class with a Dosen subclass for inheritance, and a User class with an Admin subclass showcasing polymorphism. Additionally, it features an abstract class Laporan to illustrate abstraction in PHP.

Uploaded by

Laura Anggraini
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Contoh implementasi kelas php(oop)

<?php

class Database {

private $host = "localhost";

private $user = "root";

private $pass = "";

private $db = "siakad";

protected $conn;

public function connect() {

$this->conn = new mysqli(

$this->host,

$this->user,

$this->pass,

$this->db

);

return $this->conn;

2. Enkapsulasi
class Mahasiswa {
private $nim;
private $nama;

public function setNim($nim) {


$this->nim = $nim;
}

public function getNim() {


return $this->nim;
}
}
3. Pewarisan
class User {
protected $username;
protected $password;
}

class Dosen extends User {


private $nidn;
}
4. Polimorfisme
class User {
public function login() {
return "Login user";
}
}

class Admin extends User {


public function login() {
return "Login admin";
}
}
5. Abstraksi
abstract class Laporan {
abstract public function cetak();
}

You might also like