Using PHP Class (UPC)

Before we get into the code lines, let's first understand the following important points:

1. Class

In general in the programming world, a class is a representation of an object. In PHP, a class is a collection of variables (aka properties) and functions (aka methods) that synergize with each other.

Note: aka (also known as), an English term meaning "also known as".


Examples of Using Classes in PHP

2. Rules for Writing Class

PHP creates standardization regarding classes, including:

  • Class naming must clearly represent an object, must not be an abbreviation, must not be abstract. Uncommon example: class Cxighftz; and common example: class Pacar;
  • Class names must start with a capital letter. Example: class Gadget;
  • Class names with more than 2 words can use "_" (uderscore), or not. Example: class Your_Girlfriend; or class Your Boyfriend;
  • Class names cannot be all capital letters.
  • Class declaration must be accompanied by a framework of its functions/methods. (more clearly later in the code line).
  • Class calls can include the name with "( )" or just the name. Example: Pacar(); or Pacar;

3. Constructor ( __construct(){ ... } )

It is an initialization method, which needs to be declared before the object is used.

In simple language, you need to get to know your potential girlfriend first before making her your girlfriend. Like you need to know her name, age, hobbies, house, bbm pin, facebook, etc.

4. $this->

It is a special variable that is used to access global variables, or used to refer to properties of a class.

5. Instance (Instantiation)

Used to load another library / library of a class. Its characteristics begin with "new".

Example:

$jadian = new Pacar();

6. Practical work

Okay, now it's time to get into the code. As usual, I assume that you have installed notepad++ and  wampserver .

Exercise

The second meeting of the advanced dynamic web course was about oop in php. In the learning process I experienced several error notifications, one of which was deliberately made by the lecturer to show us the principle or concept of class access.

Step 1

Create a php file named "Gadget.php" and save it to the www folder in wampserver.

Due to an error I made, the Gadget.php file code structure experienced an error, with the following notification:

Syntax writing error in php

You can see debugging for the error above in the complete code below, (marked with comments).

Complete Gadget.php Code

<?php
/*
*bundet.com
*Wawan Beneran
*OOP PHP - Cara menggunakan class
*/
 class Gadget{
  public $jenis;
  public $merek;
  public $harga;
  public $feature;
  public $tahun_prod;
 /* } <-- [ A ] sintak penutup class yang saya taruh di sini,
    adalah salah satu yang menyebabkan error dengan notifikasi:
    Parse error: syntax error, unexpected 'public' (T_Public) in... */

 public function __construct($a, $b){
  $this->harga = $a;
  $this->tahun_prod = $b;
  $this->jenis = "Magictronic";
  $this->merek = "Bimsalabim";
  $this->feature = "Melipat gandakan uang";
 }

 public function data_merek() {
  return $this->merek;
 }

 public function data_feature() {
  return $this->feature;
 }

 /*[ B ] public function data_jenis{ <-- sebelumnya, deklarasi fungsi tanpa "()"
           ini juga menyebabkan error dengan notifikasi:
      Parse error: syntax error, unexpected 'public' (T_Public) in... */

 public function data_jenis() { //[ B ] <-- yang benar seperti ini.
 return $this->jenis;
 }

} // <-- [ A ] yang benar, sintak penutup class di sini.

?>

Step 2

Create a php file named "data_gadget.php" and save it to the www folder in wampserver. In this step we are shown the error code:


Error no instantiation and class parameters in php

The problem is here:

$dataku = Gadget();

Because the Gadget class has no parameters, and the Gadget class has not been instantiated. After debugging, the result is like this:

$dataku = new Gadget($harga, $tahun);

Complete Code data_gadget.php

<?php
/*
*bundet.com
*Wawan Beneran
*OOP PHP - Cara menggunakan class
*/
 include "Gadget.php";

 $tahun= 2013;
 $harga= 17777777;
 //$dataku = Gadget(); -- Sebelumnya, penyebab error, karena tidak ada instance dan parameter.
 $dataku = new Gadget($harga, $tahun); //-- Sesudahnya, fix.

 echo "<h1>Jenis Gadget</h1>";
 echo "Jenis : " . $dataku->data_jenis();
 echo "<br />";
 echo "Merek : " . $dataku->data_merek();
 echo "<br />";
 echo "Feature : " . $dataku->data_feature();
 echo "<br />";
 echo "Harga : " . $harga;
 echo "<br />";
 echo "Tahun : " . $tahun;

?>

Running Code:


Output data_gadget.php

Exercise

Before closing or going home time, the lecturer gave us an assignment, as well as a condition for us if we wanted to go home early, we had to be able to complete it, "in the shortest possible time, FREEDOM!!!!"

Completion

Complete Code Mahasiswa.php

<?php
/*
*bundet.com
*Wawan Beneran
*OOP PHP - Cara menggunakan class [LATIHAN]
*/

/* SOAL
Buat sebuah class mahasiswa dengan construct nim
properti lain terdiri dari :
- nama
- jurusan
- matakuliah favorit

panggil class untuk menampilkan biodata
*/

 class Mahasiswa{
  public $nama;
  public $nim;
  public $jurusan;
  public $matkul_favorit;

 public function __construct($a){
  $this->nama = "Mr. Bean";
  $this->nim = $a;
  $this->jurusan = "Teknik Pantomim";
  $this->matkul_favorit = "Lip Synch";
 }

 public function data_nama(){
  return $this->nama;
 }

 public function data_jurusan(){
  return $this->jurusan;
 }

 public function data_matkul(){
  return $this->matkul_favorit;
 }

 }
?>

Complete Code data_mahasiswa.php

<?php
/*
*bundet.com
*Wawan Beneran
*OOP PHP - Cara menggunakan class [LATIHAN]
*/

 include "Mahasiswa.php";

 $nim = 111111;
 $datanya = new Mahasiswa($nim);

 echo "<h1>Mahasiswa</h1>";
 echo "Nama : " .$datanya->data_nama();
 echo "<br />";
 echo "NIM : " .$nim;
 echo "<br />";
 echo "Jurusan : " .$datanya->data_jurusan();
 echo "<br />";
 echo "Matakuliah Favorit : " .$datanya->data_matkul();

?>

Running Code:


Output data_mahasiswa.php

Reference


Post a Comment

Previous Next

نموذج الاتصال