SlideShare a Scribd company logo
1 of 32
Download to read offline
Migration from
procedural to OOP
PHPID Online Learning #6
Achmad Mardiansyah
(achmad@glcnetworks.com)
IT consultant for 15 years+
Agenda
● Introduction
● Procedural PHP
● OOP in PHP
● Migration to OOP
● QA
2
Introduction
3
About Me
4
● Name: Achmad Mardiansyah
● Base: bandung, Indonesia
● Linux user since 1999
● Write first “hello world” 1993
● First time use PHP 2004, PHP OOP 2011.
○ Php-based web applications
○ Billing
○ Radius customisation
● Certified Trainer: Linux, Mikrotik, Cisco
● Teacher at Telkom University (Bandung, Indonesia)
● Website contributor: achmadjournal.com,
mikrotik.tips, asysadmin.tips
● More info:
http://au.linkedin.com/in/achmadmardiansyah
About GLCNetworks
● Garda Lintas Cakrawala (www.glcnetworks.com)
● Based in Bandung, Indonesia
● Scope: IT Training, Web/Application developer, Network consultant (Mikrotik,
Cisco, Ubiquity, Mimosa, Cambium), System integrator (Linux based
solution), Firewall, Security
● Certified partner for: Mikrotik, Ubiquity, Linux foundation
● Product: GLC billing, web-application, customise manager
5
prerequisite
● This presentation is not for beginner
● We assume you already know basic skills of programming and algorithm:
○ Syntax, comments, variables, constant, data types, functions, conditional, loop, arrays, etc
● We assume you already have experience to create a web-based application
using procedural method
6
Procedural PHP
7
Procedural PHP
● Code executed sequentially
● Easy to understand
● Faster to implement
● Natural
● Program lines can be very long
● Need a way to architect to:
○ Manage our code physically
○ Manage our application logic
8
<?php
define DBHOST
define DBUSER
$variable1
$variable2
if (true) {
code...
}
for ($x=0; $x<=100; $x++) {
echo "The number is: $x
<br>";
Basic: Constant vs Variable
● Both are identifier to represent
data/value
● Constant:
○ Value is locked, not allowed to be
changed
○ Always static: memory address is static
● Variable:
○ Static:
■ Memory address is static
○ Dynamic
■ Memory address is dynamic
■ Value will be erased after function
is executed
9
<?php
define DBHOST
define DBUSER
$variable1
$variable2
}
?>
Basic: static vs dynamic (non-static) variable
● Static variable
○ Memory address is static
○ The value is still keep after function is
executed
● Non-static variable
○ Memory address is dynamic
○ The value is flushed after execution
10
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
Efficient code: using functions
1111
<?php
define DBHOST
define DBUSER
$variable1
$variable2
if (true) {
code...
}
for ($x=0; $x<=100; $x++) {
echo "The number is: $x
<br>";
}
1111
<?php
function calcArea () {
Code…
}
define DBHOST
define DBUSER
$variable1
$variable2
calcArea();
?>
before after
Efficient code: include
1212
<?php
function calcArea () {
Code…
}
define DBHOST
define DBUSER
$variable1
$variable2
calcArea();
?>
<?php
include file_function.php
define DBHOST
define DBUSER
$variable1
$variable2
calcArea();
?>
before after
<?php
$name = array("david", "mike", "adi", “doni”);
$arrlength = count($name);
for($x = 0; $x < $arrlength; $x++) {
echo $name[$x].”<br>”;
}
?>
Efficient code: array/list (indexed array)
<?php
$name1=david;
$name2=mike;
$name3=adi;
$name4=doni;
echo $name1.”<br>”;
echo $name2.”<br>”;
echo $name3.”<br>”;
echo $name4.”<br>”;
}
?>
1313
before after
<?php
$name = array("david"=>23, "mike"=>21,
"adi"=>25);
foreach($name as $x => $x_value) {
echo "name=".$x." age ".$x_value."<br>";
}
?>
Efficient code: Associative Arrays / dictionary
<?php
$name1=david;
$name1age=23
$name2=mike;
$name2age=21
$name3=adi;
$name3age=25
echo $name1.” ”.$name1age.”<br>”;
echo $name2.” ”.$name2age.”<br>”;
echo $name3.” ”.$name3age.”<br>”;
}
?>
1414
before after
We need more features...
● Grouping variables / functions -> so that it can represent real object
● Define access to variables/functions
● Easily extend current functions/group to have more features without losing
connections to current functions/group
15
OOP in PHP
16
● Class is a group of:
○ Variables -> attributes/properties
○ Functions -> methods
● We call the class first, and then call
what inside (attributes/methods)
● The keyword “$this” is used when a
thing inside the class, calls another
thing inside the class
CLASS → instantiate→ object
To access the things inside the class:
$object->variable
$object->method()
OOP: Class and Object
17
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->name='manggo';
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name()."<br>";
echo $banana->get_name();
?>
OOP: inheritance
● Class can have child class
● Object from child class can access
things from parent class
● Implemented in many php framework
● This is mostly used to add more
functionality of current application
● Read the documentation of the main
class
18
<?php
class Fruit {
public $name;
public $color;
public function __construct($name,
$color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry?
";
}
}
$strawberry = new
Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
OOP: method chain
● Object can access several method in
chains
● Similar to UNIX pipe functions
● For example: text processing with
various method
19
<?php
class fakeString {
private $str;
function __construct() {
$this->str = "";
}
function addA() {
$this->str .= "a";
return $this;
}
function addB() {
$this->str .= "b";
return $this;
}
function getStr() {
return $this->str;
}
}
$a = new fakeString();
echo $a->addA()->addB()->getStr();
?>
OOP: constructor
● Is a method that is executed
automatically when a class is called
20
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function get_name() {
return $this->name;
}
function get_color() {
return $this->color;
}
}
$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
OOP: destructor
● Is the method that is called when the
object is destructed or the script is
stopped or exited
21
<?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
OOP: access modifier
(attribute)
● Properties and methods can have
access modifiers which control where
they can be accessed.
● There are three access modifiers:
○ public - the property or method can be
accessed from everywhere. This is default
○ protected - the property or method can be
accessed within the class and by classes
derived from that class
○ private - the property or method can ONLY
be accessed within the class
22
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}
$mango = new Fruit();
$mango->name = 'Mango'; // OK
$mango->color = 'Yellow'; // ERROR
$mango->weight = '300'; // ERROR
?>
OOP: access modifier
(method)
● Properties and methods can have
access modifiers which control where
they can be accessed.
● There are three access modifiers:
○ public - the property or method can be
accessed from everywhere. This is default
○ protected - the property or method can be
accessed within the class and by classes
derived from that class
○ private - the property or method can ONLY
be accessed within the class
23
<?php
class Fruit {
public $name;
public $color;
public $weight;
function set_name($n) {
$this->name = $n;
}
protected function set_color($n) {
$this->color = $n;
}
private function set_weight($n) {
$this->weight = $n;
}
}
$mango = new Fruit();
$mango->set_name('Mango'); // OK
$mango->set_color('Yellow'); // ERROR
$mango->set_weight('300'); // ERROR
?>
OOP: abstract
● Abstract classes and methods are
when the parent class has a named
method, but need its child class(es) to
fill out the tasks.
● An abstract class is a class that
contains at least one abstract method.
● An abstract method is a method that is
declared, but not implemented in the
code.
● When inheriting from an abstract class,
the child class method must be defined
with the same name, and the same or
a less restricted access modifier
●
24
<?php
abstract class Car {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function intro() :
string;
}
// Child classes
class Audi extends Car {
public function intro() : string {
return "German quality! $this->name!";
}
}
// Create objects from the child classes
$audi = new audi("Audi");
echo $audi->intro();
echo "<br>";
?>
OOP: trait
● Traits are used to declare methods
that can be used in multiple classes.
● Traits can have methods and abstract
methods that can be used in multiple
classes, and the methods can have
any access modifier (public, private, or
protected).
25
<?php
trait message1 {
public function msg1() {
echo "OOP is fun! ";
}
}
class Welcome {
use message1;
}
$obj = new Welcome();
$obj->msg1();
?>
OOP: static properties
● Attached to the class
● Can be called directly without instance
● Keyword “::”
● Calling inside the class, use keyword
self::
● From child class, use keyword parent
26
<?php
class pi {
public static $value=3.14159;
public function staticValue() {
return self::$value;
}
}
class x extends pi {
public function xStatic() {
return parent::$value;
}
}
//direct access to static variable
echo pi::$value;
echo x::$value;
$pi = new pi();
echo $pi->staticValue();
?>
OOP: static method
● Attached to the class
● Can be called directly without instance
● Keyword “::”
● Calling inside the class, use keyword
self::
● From child class, use keyword parent
27
<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
public function __construct() {
self::welcome();
}
}
class SomeOtherClass {
public function message() {
greeting::welcome();
}
}
//call function without instance
greeting::welcome();
new greeting();
?>
Migration to OOP
28
Several checklist on OOP
● Step back -> planning -> coding
● Design, design, design -> architecture
○ Its like migrating to dynamic routing
○ Class design
■ Attribute
■ Method
29
OOP myth /
● Its better to learn programming directly to OOP
● Using OOP means we dont need procedural
● OOP performs better
● OOP makes programming more visual. OOP != visual programming (drag &
drop)
● OOP increases reuse (recycling of code)
●
30
QA
31
End of slides
Thank you for your attention
32

More Related Content

What's hot

Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
Kumar
 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2
Wildan Maulana
 

What's hot (20)

Php Oop
Php OopPhp Oop
Php Oop
 
Developing SOLID Code
Developing SOLID CodeDeveloping SOLID Code
Developing SOLID Code
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
 
Vision academy classes bcs_bca_bba_sybba_php
Vision academy  classes bcs_bca_bba_sybba_phpVision academy  classes bcs_bca_bba_sybba_php
Vision academy classes bcs_bca_bba_sybba_php
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHP
 
Inheritance
InheritanceInheritance
Inheritance
 
Module Magic
Module MagicModule Magic
Module Magic
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017
 
C++ oop
C++ oopC++ oop
C++ oop
 
Delegate
DelegateDelegate
Delegate
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
Inheritance
InheritanceInheritance
Inheritance
 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2
 

Similar to PHPID online Learning #6 Migration from procedural to OOP

PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
WebStackAcademy
 

Similar to PHPID online Learning #6 Migration from procedural to OOP (20)

UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Only oop
Only oopOnly oop
Only oop
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
 
Effective PHP. Part 3
Effective PHP. Part 3Effective PHP. Part 3
Effective PHP. Part 3
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
OOP Is More Than Cars and Dogs
OOP Is More Than Cars and DogsOOP Is More Than Cars and Dogs
OOP Is More Than Cars and Dogs
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2
 
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHP
 
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
Core Java Programming Language (JSE) : Chapter II - Object Oriented Programming.
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
 

More from Achmad Mardiansyah

More from Achmad Mardiansyah (20)

01 introduction to mpls
01 introduction to mpls 01 introduction to mpls
01 introduction to mpls
 
Solaris 10 Container
Solaris 10 ContainerSolaris 10 Container
Solaris 10 Container
 
Backup & Restore (BR) in Solaris OS
Backup & Restore (BR) in Solaris OSBackup & Restore (BR) in Solaris OS
Backup & Restore (BR) in Solaris OS
 
Mikrotik User Meeting Manila: bgp vs ospf
Mikrotik User Meeting Manila: bgp vs ospfMikrotik User Meeting Manila: bgp vs ospf
Mikrotik User Meeting Manila: bgp vs ospf
 
Troubleshooting load balancing
Troubleshooting load balancingTroubleshooting load balancing
Troubleshooting load balancing
 
ISP load balancing with mikrotik nth
ISP load balancing with mikrotik nthISP load balancing with mikrotik nth
ISP load balancing with mikrotik nth
 
Mikrotik firewall mangle
Mikrotik firewall mangleMikrotik firewall mangle
Mikrotik firewall mangle
 
Wireless CSMA with mikrotik
Wireless CSMA with mikrotikWireless CSMA with mikrotik
Wireless CSMA with mikrotik
 
SSL certificate with mikrotik
SSL certificate with mikrotikSSL certificate with mikrotik
SSL certificate with mikrotik
 
BGP filter with mikrotik
BGP filter with mikrotikBGP filter with mikrotik
BGP filter with mikrotik
 
Mikrotik VRRP
Mikrotik VRRPMikrotik VRRP
Mikrotik VRRP
 
Mikrotik fasttrack
Mikrotik fasttrackMikrotik fasttrack
Mikrotik fasttrack
 
Mikrotik fastpath
Mikrotik fastpathMikrotik fastpath
Mikrotik fastpath
 
Jumpstart your router with mikrotik quickset
Jumpstart your router with mikrotik quicksetJumpstart your router with mikrotik quickset
Jumpstart your router with mikrotik quickset
 
Mikrotik firewall NAT
Mikrotik firewall NATMikrotik firewall NAT
Mikrotik firewall NAT
 
Using protocol analyzer on mikrotik
Using protocol analyzer on mikrotikUsing protocol analyzer on mikrotik
Using protocol analyzer on mikrotik
 
Routing Information Protocol (RIP) on Mikrotik
Routing Information Protocol (RIP) on MikrotikRouting Information Protocol (RIP) on Mikrotik
Routing Information Protocol (RIP) on Mikrotik
 
IPv6 on Mikrotik
IPv6 on MikrotikIPv6 on Mikrotik
IPv6 on Mikrotik
 
Mikrotik metarouter
Mikrotik metarouterMikrotik metarouter
Mikrotik metarouter
 
Mikrotik firewall filter
Mikrotik firewall filterMikrotik firewall filter
Mikrotik firewall filter
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

PHPID online Learning #6 Migration from procedural to OOP

  • 1. Migration from procedural to OOP PHPID Online Learning #6 Achmad Mardiansyah (achmad@glcnetworks.com) IT consultant for 15 years+
  • 2. Agenda ● Introduction ● Procedural PHP ● OOP in PHP ● Migration to OOP ● QA 2
  • 4. About Me 4 ● Name: Achmad Mardiansyah ● Base: bandung, Indonesia ● Linux user since 1999 ● Write first “hello world” 1993 ● First time use PHP 2004, PHP OOP 2011. ○ Php-based web applications ○ Billing ○ Radius customisation ● Certified Trainer: Linux, Mikrotik, Cisco ● Teacher at Telkom University (Bandung, Indonesia) ● Website contributor: achmadjournal.com, mikrotik.tips, asysadmin.tips ● More info: http://au.linkedin.com/in/achmadmardiansyah
  • 5. About GLCNetworks ● Garda Lintas Cakrawala (www.glcnetworks.com) ● Based in Bandung, Indonesia ● Scope: IT Training, Web/Application developer, Network consultant (Mikrotik, Cisco, Ubiquity, Mimosa, Cambium), System integrator (Linux based solution), Firewall, Security ● Certified partner for: Mikrotik, Ubiquity, Linux foundation ● Product: GLC billing, web-application, customise manager 5
  • 6. prerequisite ● This presentation is not for beginner ● We assume you already know basic skills of programming and algorithm: ○ Syntax, comments, variables, constant, data types, functions, conditional, loop, arrays, etc ● We assume you already have experience to create a web-based application using procedural method 6
  • 8. Procedural PHP ● Code executed sequentially ● Easy to understand ● Faster to implement ● Natural ● Program lines can be very long ● Need a way to architect to: ○ Manage our code physically ○ Manage our application logic 8 <?php define DBHOST define DBUSER $variable1 $variable2 if (true) { code... } for ($x=0; $x<=100; $x++) { echo "The number is: $x <br>";
  • 9. Basic: Constant vs Variable ● Both are identifier to represent data/value ● Constant: ○ Value is locked, not allowed to be changed ○ Always static: memory address is static ● Variable: ○ Static: ■ Memory address is static ○ Dynamic ■ Memory address is dynamic ■ Value will be erased after function is executed 9 <?php define DBHOST define DBUSER $variable1 $variable2 } ?>
  • 10. Basic: static vs dynamic (non-static) variable ● Static variable ○ Memory address is static ○ The value is still keep after function is executed ● Non-static variable ○ Memory address is dynamic ○ The value is flushed after execution 10 function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest();
  • 11. Efficient code: using functions 1111 <?php define DBHOST define DBUSER $variable1 $variable2 if (true) { code... } for ($x=0; $x<=100; $x++) { echo "The number is: $x <br>"; } 1111 <?php function calcArea () { Code… } define DBHOST define DBUSER $variable1 $variable2 calcArea(); ?> before after
  • 12. Efficient code: include 1212 <?php function calcArea () { Code… } define DBHOST define DBUSER $variable1 $variable2 calcArea(); ?> <?php include file_function.php define DBHOST define DBUSER $variable1 $variable2 calcArea(); ?> before after
  • 13. <?php $name = array("david", "mike", "adi", “doni”); $arrlength = count($name); for($x = 0; $x < $arrlength; $x++) { echo $name[$x].”<br>”; } ?> Efficient code: array/list (indexed array) <?php $name1=david; $name2=mike; $name3=adi; $name4=doni; echo $name1.”<br>”; echo $name2.”<br>”; echo $name3.”<br>”; echo $name4.”<br>”; } ?> 1313 before after
  • 14. <?php $name = array("david"=>23, "mike"=>21, "adi"=>25); foreach($name as $x => $x_value) { echo "name=".$x." age ".$x_value."<br>"; } ?> Efficient code: Associative Arrays / dictionary <?php $name1=david; $name1age=23 $name2=mike; $name2age=21 $name3=adi; $name3age=25 echo $name1.” ”.$name1age.”<br>”; echo $name2.” ”.$name2age.”<br>”; echo $name3.” ”.$name3age.”<br>”; } ?> 1414 before after
  • 15. We need more features... ● Grouping variables / functions -> so that it can represent real object ● Define access to variables/functions ● Easily extend current functions/group to have more features without losing connections to current functions/group 15
  • 17. ● Class is a group of: ○ Variables -> attributes/properties ○ Functions -> methods ● We call the class first, and then call what inside (attributes/methods) ● The keyword “$this” is used when a thing inside the class, calls another thing inside the class CLASS → instantiate→ object To access the things inside the class: $object->variable $object->method() OOP: Class and Object 17 <?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } } $apple = new Fruit(); $banana = new Fruit(); $apple->name='manggo'; $apple->set_name('Apple'); $banana->set_name('Banana'); echo $apple->get_name()."<br>"; echo $banana->get_name(); ?>
  • 18. OOP: inheritance ● Class can have child class ● Object from child class can access things from parent class ● Implemented in many php framework ● This is mostly used to add more functionality of current application ● Read the documentation of the main class 18 <?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } public function intro() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } class Strawberry extends Fruit { public function message() { echo "Am I a fruit or a berry? "; } } $strawberry = new Strawberry("Strawberry", "red"); $strawberry->message(); $strawberry->intro(); ?>
  • 19. OOP: method chain ● Object can access several method in chains ● Similar to UNIX pipe functions ● For example: text processing with various method 19 <?php class fakeString { private $str; function __construct() { $this->str = ""; } function addA() { $this->str .= "a"; return $this; } function addB() { $this->str .= "b"; return $this; } function getStr() { return $this->str; } } $a = new fakeString(); echo $a->addA()->addB()->getStr(); ?>
  • 20. OOP: constructor ● Is a method that is executed automatically when a class is called 20 <?php class Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } function get_name() { return $this->name; } function get_color() { return $this->color; } } $apple = new Fruit("Apple", "red"); echo $apple->get_name(); echo "<br>"; echo $apple->get_color(); ?>
  • 21. OOP: destructor ● Is the method that is called when the object is destructed or the script is stopped or exited 21 <?php class Fruit { public $name; public $color; function __construct($name) { $this->name = $name; } function __destruct() { echo "The fruit is {$this->name}."; } } $apple = new Fruit("Apple"); ?>
  • 22. OOP: access modifier (attribute) ● Properties and methods can have access modifiers which control where they can be accessed. ● There are three access modifiers: ○ public - the property or method can be accessed from everywhere. This is default ○ protected - the property or method can be accessed within the class and by classes derived from that class ○ private - the property or method can ONLY be accessed within the class 22 <?php class Fruit { public $name; protected $color; private $weight; } $mango = new Fruit(); $mango->name = 'Mango'; // OK $mango->color = 'Yellow'; // ERROR $mango->weight = '300'; // ERROR ?>
  • 23. OOP: access modifier (method) ● Properties and methods can have access modifiers which control where they can be accessed. ● There are three access modifiers: ○ public - the property or method can be accessed from everywhere. This is default ○ protected - the property or method can be accessed within the class and by classes derived from that class ○ private - the property or method can ONLY be accessed within the class 23 <?php class Fruit { public $name; public $color; public $weight; function set_name($n) { $this->name = $n; } protected function set_color($n) { $this->color = $n; } private function set_weight($n) { $this->weight = $n; } } $mango = new Fruit(); $mango->set_name('Mango'); // OK $mango->set_color('Yellow'); // ERROR $mango->set_weight('300'); // ERROR ?>
  • 24. OOP: abstract ● Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. ● An abstract class is a class that contains at least one abstract method. ● An abstract method is a method that is declared, but not implemented in the code. ● When inheriting from an abstract class, the child class method must be defined with the same name, and the same or a less restricted access modifier ● 24 <?php abstract class Car { public $name; public function __construct($name) { $this->name = $name; } abstract public function intro() : string; } // Child classes class Audi extends Car { public function intro() : string { return "German quality! $this->name!"; } } // Create objects from the child classes $audi = new audi("Audi"); echo $audi->intro(); echo "<br>"; ?>
  • 25. OOP: trait ● Traits are used to declare methods that can be used in multiple classes. ● Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected). 25 <?php trait message1 { public function msg1() { echo "OOP is fun! "; } } class Welcome { use message1; } $obj = new Welcome(); $obj->msg1(); ?>
  • 26. OOP: static properties ● Attached to the class ● Can be called directly without instance ● Keyword “::” ● Calling inside the class, use keyword self:: ● From child class, use keyword parent 26 <?php class pi { public static $value=3.14159; public function staticValue() { return self::$value; } } class x extends pi { public function xStatic() { return parent::$value; } } //direct access to static variable echo pi::$value; echo x::$value; $pi = new pi(); echo $pi->staticValue(); ?>
  • 27. OOP: static method ● Attached to the class ● Can be called directly without instance ● Keyword “::” ● Calling inside the class, use keyword self:: ● From child class, use keyword parent 27 <?php class greeting { public static function welcome() { echo "Hello World!"; } public function __construct() { self::welcome(); } } class SomeOtherClass { public function message() { greeting::welcome(); } } //call function without instance greeting::welcome(); new greeting(); ?>
  • 29. Several checklist on OOP ● Step back -> planning -> coding ● Design, design, design -> architecture ○ Its like migrating to dynamic routing ○ Class design ■ Attribute ■ Method 29
  • 30. OOP myth / ● Its better to learn programming directly to OOP ● Using OOP means we dont need procedural ● OOP performs better ● OOP makes programming more visual. OOP != visual programming (drag & drop) ● OOP increases reuse (recycling of code) ● 30
  • 31. QA 31
  • 32. End of slides Thank you for your attention 32