You are on page 1of 28

Pendidikan Komputer

Fakultas Keguruan dan Ilmu Pendidikan


Universitas Lambung Mangkurat

Oleh
PHP (ARRAY, FUNCTION DAN OOP) Dr. Harja Santana Purba, M.Kom
Novan Alkaf B. S., S.Kom., M.T

Minggu ke-3: 7 Maret 2023


CATATAN TUGAS 1
ØSebagian tugas sudah dikerjakan dengan baik namun ada
beberapa kekurangan
ØBeberapa mahasiswa Copy/Paste Tugas Kelompok hanya ganti Nama
ØKesesuaian UI perlu diperhatikan (Icon, Letak, Font)
ØCara Meletakkan Source Code harus Rapi (Sebaiknya bukan Gambar)
ØPerlu memahami benar pengerjaan tugas!
Capaian Pembelajaran
ØMahasiswa menjelaskan Dasar Bahasa Pemrograman PHP

ARRAY FUNCTION

OOP
1. ARRAY
oAn array in PHP is actually an ordered map.
oA map is a type that associates values to keys.
oThis type is optimized for several different uses; it can be treated as an array, list
(vector), hash table (an implementation of a map), dictionary, collection, stack,
queue, and probably more.
oAs array values can be other arrays, trees and multidimensional arrays are also
possible.
ohttps://www.php.net/manual/en/language.types.array.php
ARRAY
ARRAY
METHOD USEFUL IN ARRAY
array_intersect() /
COUNT array_diff() / array_diff_assoc()
array_intersect_assoc()

array_pop() array_push() array_search()

array_slice() array_splice() array_sum()

array_unique() array_unshift() sort()

https://www.w3schools.com/php/php_ref_array.asp
ARRAY – EXERCISE (PART 1)
1. If you have an array $a = array( 0, 1, 2, 3, 4 ); , how do you extract the value 3
from the array?
2. If you have an array $a = array( "zero"=>0, "one"=>1, "two"=>2, "three"=>3,
"four"=>4 );, how do you extract the value 3 from the array?
ARRAY – EXERCISE (PART 2)
3. If you have the following array, how do
you extract the value 3 from the array?
ARRAY – EXERCISE (PART 3)
4. If you have the following array, how do
you extract the value 3 from the array?
ARRAY – EXERCISE (PART 4)
5. Create a new array with each comma-separated value as its own array element from the
string $a = "a,b,c,d,e,f”
6. With the result array from 5, create a new array where each element is both key and value.
In other words, the result should be:
ARRAY – EXERCISE (PART 5)
7. You have two arrays like the following. One
contains field labels, the other contains field
values. Write a program to output the third
array.
ARRAY – EXERCISE (PART 6)
8. You have an array of transactions,
each of which has a debit and credit
amount. Find the absolute value of
the transaction amount
(i.e. abs( debit - credit )) and add it
as a new key=>value pair to each
transaction.
2. FUNCTION
oThe real power of PHP comes from its functions.
oPHP has more than 1000 built-in functions, and in
addition you can create your own custom
functions.
Besides the built-in PHP functions, it is possible to
create your own functions.
1. A function is a block of statements that can be used
repeatedly in a program.
2. A function will not execute automatically when a page
loads
3. A function will be executed by a call to the function.
2. FUNCTION
FUNCTION – EXERCISE (PART 1)
1. Write a function to calculate the factorial of a number (a non-negative integer)
2. Write a program in PHP to print prime numbers between 1 and 100.
3. Write a function to reverse a string!
4. Write a PHP function that checks whether a passed string is a palindrome or not?
EXERCISE PART 2
Buatlah program menghitung barang dengan ketentuan berikut:

Buat 2 Function, 1 function hitung_total()


dan hitung_diskon(). Hitung diskon akan
dipanggil didalam hitung_total() untuk
menghasilkan harga akhir.

18
PENDAHULUAN
3. PEMBAHASAN( PROSEDURAL)
OOP
PENDAHULUAN ( PROSEDURAL)
3. PENDAHULUAN PROSEDURAL
Prosedural Programming:
­ Instruksi dilakukan Langkah demi Langkah
­ Memecah program menjadi bagian-bagian kecil
­ Bagian kecil: disebut prosedur, subroutine atau function
­ Linear / Top-to-Bottom
­ Fortran, ALGOL, COBOL, Pascal, C, PHP, Javascript
KENAPA PROSEDURAL
3. KENAPA ?
PROSEDURAL?
To-the-point
Simplicity & kemudahan impkementasi ( untuk compiler & interpreter)
Mudah ditelusuri
Membutuhkan lebih sedikit memory ( dibandingkan OOP)
OBJECT ORIENTED PROGRAMMING
3. OBJECT-ORIENTED PROGRAMMING
Menyusun semua kode program dan struktur data sebagai objek
­ Semua akan dianggap sebagai objek/intetas

Objek adalah unit dasar dari program


Objek menyimpan data (property) dan perilaku ( method)
­ Cenderung tidak punya variable dan function

Objeck bisa saling berinteraksi


Java, Ruby, Python, C++, Javascript dan PHP5 keatas
KELEBIHAN OOPOOP
3. KELEBIHAN
Mudah merepresentasikan dunia nyata
Enkapsulasi dan Abstraksi Data
­ Enkapsulasi : Data menjadi aman, data hanya bisa diakses oleh kode tertentu.
­ Abstraksi data: mudah untuk menyembunyikan kerumitan program

Reusability
Skalabilitas dan Ekstensibiltas
Kemudahan Pengelolaan
Kolaborasi
Digunakan oleh framework
PROCEDURAL VS OBJECT
3. PROSEDURAL VS OOP ORIENTED
Simple dan Self-work: Prosedural
Besar dan team-work: OOP
KONSEP OOPOOP
3. KONSEP (PHP)
Basic
­ Class dan Object
­ Property & Method
­ Constructor
­ Object Type ( objek sebagai tipe data tersendiri)
­ Inheritance
­ Visibility / Access Modifier ( public, private, protected)
­ Setter & Getter
­ Static Method
KONSEP OOPOOP
3. KONSEP (PHP)
Advanced
­ Abstract dan Interface
­ Interceptor -> Setter & Getter -> Magic Method
­ Object Cloning
­ Callback dan Closures
­ Namespaces & Autoloading ( loading semua kelas kedalam program)
3. OBJECT-ORIENTED PROGRAMMING (W3SCHOOLS)
oImportant Concept
o CLASS and OBJECT
o CONSTRUCTOR
o DECONSTRUCTOR
o ACCESS MODIFIER
o CONSTANT
o ABSTRACT CLASS
o INTERFACES
o TRAIT
o STATIC METHOD
o STATIC PROPERTIES
o NAMESPACE
MINGGU DEPAN ?

1. FORM PHP
2. KONEKSI PHP DAN MySQL
3. CRUD dengan PHP dan MySQL
DAFTAR PUSTAKA
ohttps://www.php.net/manual/en/
oInternet & World Wide Web How to Program, 5th Editon, Deitel Deitel Goldberg
oPHP 8 Basic, Gunnard Engeberth & Satej Kumar Sahu, Apress, 2023
oPro PHP 8 MVC, Christoper Pitt, Apress, 2021
ohttps://gist.github.com/dahjelle/ceedd4881cd21dddb935
ohttps://tutorialsclass.com/exercises/php/php-array/
oOOP dengan PHP: https://www.w3schools.com/php/php_oop_what_is.asp

You might also like