SlideShare a Scribd company logo
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

Php Oop
Php OopPhp Oop
Php Oop
mussawir20
 
Developing SOLID Code
Developing SOLID CodeDeveloping SOLID Code
Developing SOLID Code
Mark Niebergall
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
Jason Austin
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna 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] 2016
Chris 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 OOP
Wildan 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 2017
Chris 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_php
sachin892777
 
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 PHP
David Stockton
 
Inheritance
InheritanceInheritance
Inheritance
Burhan Ahmed
 
Module Magic
Module MagicModule Magic
Module Magic
James Gray
 
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
Alena Holligan
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
Delegate
DelegateDelegate
Delegate
rsvermacdac
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
Kumar
 
Inheritance
InheritanceInheritance
Inheritance
Jancirani Selvam
 
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

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
Dhivyaa C.R
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
Atikur Rahman
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
Atikur Rahman
 
Effective PHP. Part 3
Effective PHP. Part 3Effective PHP. Part 3
Effective PHP. Part 3
Vasily Kartashov
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
Alena Holligan
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
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
Chris Tankersley
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
Denis 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 gtk2
Elizabeth 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 PHP
Alena 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
 
Drupal 8 migrate!
Drupal 8 migrate!Drupal 8 migrate!
Drupal 8 migrate!
Pavel Makhrinsky
 

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

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

01 introduction to mpls
01 introduction to mpls 01 introduction to mpls
01 introduction to mpls
Achmad Mardiansyah
 
Solaris 10 Container
Solaris 10 ContainerSolaris 10 Container
Solaris 10 Container
Achmad Mardiansyah
 
Backup & Restore (BR) in Solaris OS
Backup & Restore (BR) in Solaris OSBackup & Restore (BR) in Solaris OS
Backup & Restore (BR) in Solaris OS
Achmad Mardiansyah
 
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
Achmad Mardiansyah
 
Troubleshooting load balancing
Troubleshooting load balancingTroubleshooting load balancing
Troubleshooting load balancing
Achmad Mardiansyah
 
ISP load balancing with mikrotik nth
ISP load balancing with mikrotik nthISP load balancing with mikrotik nth
ISP load balancing with mikrotik nth
Achmad Mardiansyah
 
Mikrotik firewall mangle
Mikrotik firewall mangleMikrotik firewall mangle
Mikrotik firewall mangle
Achmad Mardiansyah
 
Wireless CSMA with mikrotik
Wireless CSMA with mikrotikWireless CSMA with mikrotik
Wireless CSMA with mikrotik
Achmad Mardiansyah
 
SSL certificate with mikrotik
SSL certificate with mikrotikSSL certificate with mikrotik
SSL certificate with mikrotik
Achmad Mardiansyah
 
BGP filter with mikrotik
BGP filter with mikrotikBGP filter with mikrotik
BGP filter with mikrotik
Achmad Mardiansyah
 
Mikrotik VRRP
Mikrotik VRRPMikrotik VRRP
Mikrotik VRRP
Achmad Mardiansyah
 
Mikrotik fasttrack
Mikrotik fasttrackMikrotik fasttrack
Mikrotik fasttrack
Achmad Mardiansyah
 
Mikrotik fastpath
Mikrotik fastpathMikrotik fastpath
Mikrotik fastpath
Achmad Mardiansyah
 
Jumpstart your router with mikrotik quickset
Jumpstart your router with mikrotik quicksetJumpstart your router with mikrotik quickset
Jumpstart your router with mikrotik quickset
Achmad Mardiansyah
 
Mikrotik firewall NAT
Mikrotik firewall NATMikrotik firewall NAT
Mikrotik firewall NAT
Achmad Mardiansyah
 
Using protocol analyzer on mikrotik
Using protocol analyzer on mikrotikUsing protocol analyzer on mikrotik
Using protocol analyzer on mikrotik
Achmad Mardiansyah
 
Routing Information Protocol (RIP) on Mikrotik
Routing Information Protocol (RIP) on MikrotikRouting Information Protocol (RIP) on Mikrotik
Routing Information Protocol (RIP) on Mikrotik
Achmad Mardiansyah
 
IPv6 on Mikrotik
IPv6 on MikrotikIPv6 on Mikrotik
IPv6 on Mikrotik
Achmad Mardiansyah
 
Mikrotik metarouter
Mikrotik metarouterMikrotik metarouter
Mikrotik metarouter
Achmad Mardiansyah
 
Mikrotik firewall filter
Mikrotik firewall filterMikrotik firewall filter
Mikrotik firewall filter
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

GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
Severalnines
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 

Recently uploaded (20)

GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 

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