CRUD PHP - Konsep OOP
Laporan Praktikum

17 Mei 2025 ~ Oleh Surya Andika

Konsep OOP

  • Class − a template for making many instances of the same kind (or class) of object.
  • Object − individual instance of the data structure defined by a class.
  • Member Variable − These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created. object data. variables of a parent class. or super class.
  • Member function − These are the function defined inside a class and are used to access.
  • Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and.
  • Parent class − A class that is inherited from by another class. This is also called a base class.
  • Child Class − A class that inherits from another class. This is also called a subclass or derived class.
  • Polymorphism − Same function can be used for different purposes.
  • Overloading − a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.
  • Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted).
  • Encapsulation − refers to a concept where we encapsulate all the data and member functions together to form an object.
  • Constructor − refers to a special type of function which will be called automatically whenever there is an object formation from a class.
  • Destructor − refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

Langkah-langkah Membuat CRUD Sederhana OOP PHP dan MySQL

1. Tujuan

Agar mahasiswa mampu mengimplementasikan konsep OOP pada PHP dengan membuat suatu aplikasi CRUD sederhana.


2. Alat yang digunakan

  • XAMPP (SQL)
  • Visual Studio Code (code editor)
  • Git (jika perlu)

3. Langkah-langkah

A. Persiapan

  • Pertama buat folder baru crud di dalam folder htdocs yang ada pada folder XAMPP
  • Kemudian buat beberapa file dan folder seperti berikut di dalam folder crud sebelumnya.
  • List Folder CRUD
  • Download dan simpan file css dan js Bootstrap kemudian masukkan hasil download css dan js ke dalam folder assets.

B. Koneksi ke Database

  • Buat database pemrograman_web dengan menggunakan kode SQL kemudian buat juga tabel mahasiswa.
  • Buat Database Buat Tabel
  • Buka file Database.php pada folder config lalu buat kode berikut untuk membuat koneksi ke database
  • Koneksi Database

C. File Config

  • Buka file Config.php pada folder config lalu tambahkan kode variabel BASE_URL ini agar nanti bisa diakses dari kelas manapun.
  • Config BASE URL

D. Alert

  • Buka file Alert.php pada folder function lalu buat kode berikut untuk menampilkan pesan berhasil/gagalnya aktifitas yang dijalankan.
  • Alert

E. Class Model Mahasiswa

  • Buka file mahasiswa.php pada folder model lalu tambahkan fungsi session_start() dan juga class Mahasiswa yang berisi deklarasi variabel-variabel yang nanti akan dibutuhkan.
  • Tambahkan fungsi constructor serta operasi Create, Read, Update dan Delete di dalamnya.
  • Model Mahasiswa

F. Class Function Mahasiswa

  • Buat file mahasiswa.php pada folder function lalu buat kode ini.
  • Panggil file Database.php, Config.php, dan model/mahasiswa.php dengan menggunakan fungsi include atau include_once
  • Kemudian buat objek new Database dan new mahasiswa yang akan disambungkan koneksinya.
  • Tambahkan kode program $_GET['action'] ini pada kode sebelumnya.
  • Function Mahasiswa

G. View

  • Index.php: file ini akan menampilkan data-data mahasiswa yang sudah berhasil di input.
  • Code Index Result Index
  • Create.php: file ini adalah tempat untuk menambahkan data mahasiswa.
  • Code Create Result Create
  • Edit.php: file ini adalah tempat pengguna mengedit data mahasiswa yang sudah di input sebelumnya.
  • Code Edit Result Edit