25.04.12 Web Programming-even-2012-dww 1
OO in PHP
-Framework,MVC,CMS
(From several books)
25.04.12 Web Programming-even-2012-dww 2
● Dalam prosedural prog → usually use lib
(library)
● Lib → contains related function
Ex: dbfunction.inc → grup function terkait pengelolaan database (whole)
● OO dlm PHP dikenalkan sebagai ”whole”
25.04.12 Web Programming-even-2012-dww 3
● PHP also support OO (object oriented)
eventhough a simple OO → PHP keep simple, no need to make it so complex.
● OO in PHP is not like to turn to Java-like
● OO very important for big program → good for
organizing
● Recall concept of OO (class, inheritance...). ● Pemahaman dasar tentang OOP plg penting,
25.04.12 Web Programming-even-2012-dww 4
● OO bisa diartikan segala sesuatu yang
bersifat modular. Modularisasi
● Object → gambaran di dunia nyata, ex:
baju gaun. Class → representasi object dalam program, ex: desain baju gaun
● Class scr sederhana sbg the other
non-scalar data type.
● Recall array (index and assosiatif), the
complex data type as the prominent about class, all basic dari php
25.04.12 Web Programming-even-2012-dww 5
● PHP 5 start to create this way (OO),
mengenalkan SPL (Standart PHP Library), berisi class yang siap digunakan
Ex:
– SQLite → class_lib dlm pengelolaan
databases
– libxml2 → class_lib API dlm pengelolaan
25.04.12 Web Programming-even-2012-dww 6
Class
● Class is a storage to store data and information
of object, property or attributes of object as like as a variable
● In class not only for property but also a
function. Function in a class show the process that used to in object
● class classname{
//declare of property or/and function }
25.04.12 Web Programming-even-2012-dww 7
Ex:
● Class Course in IE dept
● Has 3 variables-properties (dept, code
and name), general attributes in object course
25.04.12 Web Programming-even-2012-dww 8
● U can think class as ”a factory knife” can cut
uniformly or cut with various style and keep the core properties
● Sederhananya, class memproduksi banyak
25.04.12 Web Programming-even-2012-dww 9
● Or define value of var ●
25.04.12 Web Programming-even-2012-dww 10
25.04.12 Web Programming-even-2012-dww 11
● Class not only storage of properties but also
functions/methods (process its own data)
25.04.12 Web Programming-even-2012-dww 12
25.04.12 Web Programming-even-2012-dww 13
25.04.12 Web Programming-even-2012-dww 14
● Dari contoh sebelumnya class yang telah
dibuat dapat dikatakan sebagai sebuah modul.
● Lets name it Class_Student_lib.php
25.04.12 Web Programming-even-2012-dww 15
Constructor
25.04.12 Web Programming-even-2012-dww 16
Encapsulation
● Simply, make boundary of the class, membuat
aturan batasan akses
● Modifiers: Public, Private, Protected ● Default: public
● ”var” → public
● Public: have no access restrictions, meaning
anyone can access them. Tidak ada pembatasan
25.04.12 Web Programming-even-2012-dww 17
● Private: only the same class can access.
Hanya dalam class tersebut
● Protected: only the same class and
classes derived from that class can
access. Hanya dalam class tersebut dan class turunanny
● Gunanya: pembatasan pengaksesan
class
25.04.12 Web Programming-even-2012-dww 18
25.04.12 Web Programming-even-2012-dww 20
Inheritance
● One of important concept in OO, modify prev ex
● Make inheritance, pewarisan
● class classname2 extends classname{
//add properties or functions }
25.04.12 Web Programming-even-2012-dww 22
25.04.12 Web Programming-even-2012-dww 23
25.04.12 Web Programming-even-2012-dww 25
25.04.12 Web Programming-even-2012-dww 27
25.04.12 Web Programming-even-2012-dww 28
25.04.12 Web Programming-even-2012-dww 30
● Sometimes (when using inheritance,)
anda ingin customize methods dasar dari core class
● Lakukan ”override” atas methods core
● Ex: function show1 di class Student will be
25.04.12 Web Programming-even-2012-dww 32
Memulai pendekatan OO
● Definisikan problem utama, kemudian ke
sub problem sampai batas yg terkecil yg diinginkan → spesific task, but keep it
simple
● Create simple analysis
– Object didefiniskan problem real world – Buat relasi antar object
– Buat class yg mungkin (sekaligus
properties dan methods)
– Perbaiki kebiasaan Naming, comment →
25.04.12 Web Programming-even-2012-dww 33
● Then U can make Ur own class to manage
database, to make connection, or to make validation, a template for website, etc
● OO in PHP make it more dynamic and easier to
manage and program
● All U need only improve from the basic and
25.04.12 Web Programming-even-2012-dww 34
Ex:
● Problem input
● Subproblem/task → objek yang mungkin
ada
– Form
– Generate captcha code – Validasi input
25.04.12 Web Programming-even-2012-dww 35
Ex:
● Problem Database MySQL→ berulang
penggunaannya
● Sub problem/task → object yg mungkin
– Connection – Database – Table
25.04.12 Web Programming-even-2012-dww 36
25.04.12 Web Programming-even-2012-dww 37
What is it?
● High level OO, simply web based S/W ● Adalah paket S/W yg berisi code untuk
fungsi umum/general, di mana user dapat melakukan customization (specialization, overriden etc)
● Web Framework adalah s/w yg di desain
scr umum untuk Web applications and Web services
25.04.12 Web Programming-even-2012-dww 38
MVC
● Model- View – Controller → konsep dasar
yg sering digunakan di framework atau bentuk modularisasi yg lain
● Model →data, proses (Database, Web
Services ...)
● View →penyajian Model ke dalam bentuk
interaksi dengan user (Layout..)
● Controller → mengatur input dan respon
antara Model dan View (http, html, xml, json, ajax...)
25.04.12 Web Programming-even-2012-dww 40
● A lot, web framework based on PHP
– Zend (mature one) – Symfony
– CakePHP – Spring
– Django – etc
25.04.12 Web Programming-even-2012-dww 41
● Easy to use, easy to learn, easy to modify ● All U need,
– invest time to read the manual, – the packet their have
– using oo – skill to customize the
25.04.12 Web Programming-even-2012-dww 42
Ex:
● Zend Framework has a bunch core PHP
Applications
● Ex:
– Database access (Zend_DB)
– Google Data API’s (Zend_Gdata) – OpenID (Zend_OpenId)
– etc
● All U need is configure, know the basic of
PHP and OO concept in PHP to learn and use it
25.04.12 Web Programming-even-2012-dww 43
CMS
(Content Management
System)
25.04.12 Web Programming-even-2012-dww 44
● CMS more about concept than product ● Tepatnya Web CMS
● Besar kecilnya relatif dengan its aim ● Used to for Large Scale Website
● Ex:
– Moodle → for e-learning – Drupal → general website – Etc
● Must be dynamic, a bit complex ● Much modularization, OO
25.04.12 Web Programming-even-2012-dww 45
● Categories:
– Authoring, penanganan user pengisi content – Workflow, pengaturan alur dari authoring ke
publishing
– Storage, pengiriman atau penyimpanan ke
storage
– Publishing, penyajian di webpage
● Core features: – User management – User Interface – Application – Data Source – Deployment
25.04.12 Web Programming-even-2012-dww 46
● Simple Ex: CMS Homepage Article
– Installation – Log in Admin
– Initiatiation of database/ data file
– Interface Management article (list article,
add,edit,delete-op)
– Admin interface to add content of article
(author, date/time, title, category, body article, etc)
25.04.12 Web Programming-even-2012-dww 47
25.04.12 Web Programming-even-2012-dww 48
● Java memeliki banyak kelebihan, atau
dapat melakukan hal lebih baik dari PHP
● PHP has to be the wide range of
extensions available for the language
● PHP+ Java → menggunakan Java (Java
very powerfull in lib) untuk support aplikasi web bases (PHP)
25.04.12 Web Programming-even-2012-dww 49
● By default PHP isnt ready working with
Java
● Only need to add configuration (depend
on the platform), *.nix or windows
● Assumption in windows, U have installed
properly java and take a look in PHP directory, make sure that U have
php_java.dll and php_jar.dll.
25.04.12 Web Programming-even-2012-dww 50
● Then repaire the configuration. Open
PHP.INI under PHP directory
● Make all [Java] section enabled and
correct
25.04.12 Web Programming-even-2012-dww 51
● After those steps, Java will be ready as a
class for PHP
● Usually as library → API
● And could access Java from PHP Scripts,