SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
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
11
11
<?php
define DBHOST
define DBUSER
$variable1
$variable2
if (true) {
code...
}
for ($x=0; $x<=100; $x++) {
echo "The number is: $x
<br>";
}
11
11
<?php
function calcArea () {
Code…
}
define DBHOST
define DBUSER
$variable1
$variable2
calcArea();
?>
before after
Efficient code: include
12
12
<?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>”;
}
?>
13
13
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>”;
}
?>
14
14
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

Más contenido relacionado

La actualidad más candente

Software project management
Software project managementSoftware project management
Software project management
R A Akerkar
 

La actualidad más candente (20)

Software re engineering
Software re engineeringSoftware re engineering
Software re engineering
 
Introduction to software engineering
Introduction to software engineeringIntroduction to software engineering
Introduction to software engineering
 
Software Evolution and Maintenance Models
Software Evolution and Maintenance ModelsSoftware Evolution and Maintenance Models
Software Evolution and Maintenance Models
 
Software Re-Engineering
Software Re-EngineeringSoftware Re-Engineering
Software Re-Engineering
 
Formal Approaches to SQA.pptx
Formal Approaches to SQA.pptxFormal Approaches to SQA.pptx
Formal Approaches to SQA.pptx
 
Software project management
Software project managementSoftware project management
Software project management
 
software re-engineering
software re-engineeringsoftware re-engineering
software re-engineering
 
Software reverse engineering
Software reverse engineeringSoftware reverse engineering
Software reverse engineering
 
Capability Maturity Model (CMM) in Software Engineering
Capability Maturity Model (CMM) in Software EngineeringCapability Maturity Model (CMM) in Software Engineering
Capability Maturity Model (CMM) in Software Engineering
 
Requirements engineering
Requirements engineeringRequirements engineering
Requirements engineering
 
Software process
Software processSoftware process
Software process
 
Software Evolution
Software EvolutionSoftware Evolution
Software Evolution
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
Software Evolution
Software EvolutionSoftware Evolution
Software Evolution
 
Program activation records
Program activation recordsProgram activation records
Program activation records
 
Advanced Software Engineering.ppt
Advanced Software Engineering.pptAdvanced Software Engineering.ppt
Advanced Software Engineering.ppt
 
closure properties of regular language.pptx
closure properties of regular language.pptxclosure properties of regular language.pptx
closure properties of regular language.pptx
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Software Re-engineering Forward & Reverse Engineering
Software Re-engineering Forward & Reverse EngineeringSoftware Re-engineering Forward & Reverse Engineering
Software Re-engineering Forward & Reverse Engineering
 

Similar a Migration from Procedural to OOP

PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 

Similar a Migration from Procedural to OOP (20)

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
 
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
 
Only oop
Only oopOnly oop
Only oop
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
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
 
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
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
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
 
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
 
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
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 

Más de GLC Networks

Más de GLC Networks (20)

Firewall mangle PBR: steering outbound path similar to inbound
Firewall mangle PBR: steering outbound path similar to inboundFirewall mangle PBR: steering outbound path similar to inbound
Firewall mangle PBR: steering outbound path similar to inbound
 
Internal BGP tuning: Mesh peering to avoid loop
Internal BGP tuning: Mesh peering to avoid loopInternal BGP tuning: Mesh peering to avoid loop
Internal BGP tuning: Mesh peering to avoid loop
 
BGP tuning: Peer with loopback
BGP tuning: Peer with loopbackBGP tuning: Peer with loopback
BGP tuning: Peer with loopback
 
BGP security tuning: pull-up route
BGP security tuning: pull-up routeBGP security tuning: pull-up route
BGP security tuning: pull-up route
 
BGP troubleshooting: route origin
BGP troubleshooting: route originBGP troubleshooting: route origin
BGP troubleshooting: route origin
 
Steering traffic in OSPF: Interface cost
Steering traffic in OSPF: Interface costSteering traffic in OSPF: Interface cost
Steering traffic in OSPF: Interface cost
 
Tuning OSPF: Bidirectional Forwarding Detection (BFD)
Tuning OSPF: Bidirectional Forwarding Detection (BFD)Tuning OSPF: Bidirectional Forwarding Detection (BFD)
Tuning OSPF: Bidirectional Forwarding Detection (BFD)
 
Tuning OSPF: Prefix Aggregate
Tuning OSPF: Prefix AggregateTuning OSPF: Prefix Aggregate
Tuning OSPF: Prefix Aggregate
 
Tuning OSPF: area hierarchy, LSA, and area type
Tuning OSPF:  area hierarchy, LSA, and area typeTuning OSPF:  area hierarchy, LSA, and area type
Tuning OSPF: area hierarchy, LSA, and area type
 
Stable OSPF: choosing network type.pdf
Stable OSPF: choosing network type.pdfStable OSPF: choosing network type.pdf
Stable OSPF: choosing network type.pdf
 
Controlling Access Between Devices in the same Layer 2 Segment
Controlling Access Between Devices in the same Layer 2 SegmentControlling Access Between Devices in the same Layer 2 Segment
Controlling Access Between Devices in the same Layer 2 Segment
 
GIT as Mikrotik Configuration Management
GIT as Mikrotik Configuration ManagementGIT as Mikrotik Configuration Management
GIT as Mikrotik Configuration Management
 
RouterOS Migration From v6 to v7
RouterOS Migration From v6 to v7RouterOS Migration From v6 to v7
RouterOS Migration From v6 to v7
 
Building a Web Server with NGINX
Building a Web Server with NGINXBuilding a Web Server with NGINX
Building a Web Server with NGINX
 
Best Current Practice (BCP) 38 Ingress Filtering for Security
Best Current Practice (BCP) 38 Ingress Filtering for SecurityBest Current Practice (BCP) 38 Ingress Filtering for Security
Best Current Practice (BCP) 38 Ingress Filtering for Security
 
EOIP Deep Dive
EOIP Deep DiveEOIP Deep Dive
EOIP Deep Dive
 
Policy Based Routing with Indirect BGP - Part 2
Policy Based Routing with Indirect BGP - Part 2Policy Based Routing with Indirect BGP - Part 2
Policy Based Routing with Indirect BGP - Part 2
 
Policy Based Routing with Indirect BGP - Part 1
Policy Based Routing with Indirect BGP - Part 1Policy Based Routing with Indirect BGP - Part 1
Policy Based Routing with Indirect BGP - Part 1
 
Internet Protocol Deep-Dive
Internet Protocol Deep-DiveInternet Protocol Deep-Dive
Internet Protocol Deep-Dive
 
Network Monitoring with The Dude and Whatsapp
Network Monitoring with The Dude and WhatsappNetwork Monitoring with The Dude and Whatsapp
Network Monitoring with The Dude and Whatsapp
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 

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 11 11 <?php define DBHOST define DBUSER $variable1 $variable2 if (true) { code... } for ($x=0; $x<=100; $x++) { echo "The number is: $x <br>"; } 11 11 <?php function calcArea () { Code… } define DBHOST define DBUSER $variable1 $variable2 calcArea(); ?> before after
  • 12. Efficient code: include 12 12 <?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>”; } ?> 13 13 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>”; } ?> 14 14 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