SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5Jason Austin
 
Oop in-php
Oop in-phpOop in-php
Oop in-phpRajesh S
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
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] 2016Chris Tankersley
 
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 OOPWildan Maulana
 
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 2017Chris Tankersley
 
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_phpsachin892777
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)Jyoti shukla
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHPDavid Stockton
 
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] 2017Alena Holligan
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classesKumar
 
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 #2Wildan Maulana
 

Was ist angesagt? (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
 

Ähnlich wie PHPID online Learning #6 Migration from procedural to OOP

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 CollegeDhivyaa C.R
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxAtikur Rahman
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxDavidLazar17
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Alena Holligan
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxAtikur Rahman
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Alena Holligan
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overviewjsmith92
 
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 DogsChris Tankersley
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxShaownRoy1
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboardsDenis Ristic
 
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 gtk2Elizabeth Smith
 
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 PHPAlena Holligan
 
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
 

Ähnlich wie 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!
 

Mehr von Achmad Mardiansyah

Mehr von 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
 

Kürzlich hochgeladen

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 

Kürzlich hochgeladen (20)

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 

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