SlideShare a Scribd company logo
AN INTRODUCTION TO
OBJECT-ORIENTED
PROGRAMMING (OOP)
I am , , and .Xano @BartFeenstra http://mynameisbart.com
WHAT IS OBJECT-ORIENTED
PROGRAMMING?
The form of programming that uses instances (objects) of classes
(predefined data types) to structure (group) information and functionality.
GLOSSARY
Class
A predefined type (blueprint) of complex data.
Object
An instance of a class; a single unit of complex data.
Property
A variable that belongs to a class or object.
Method
A function that belongs to a class or object.
STATE
Objects have state (internal configuration) which can be changed.
Most, if not all usages of static variables in Drupal 7 have been replaced with
object properties in Drupal 8.
INTERFACES
Interfaces define what objects must be able to do,
but do not define how they must do these things.
They are contracts that must be fulfilled.
<?php
interface FooInterface {
public function doFoo($foo);
}
interface FooMultipleInterface extends FooInterface {
public function doFooMultiple(array $foos);
}
CLASSES
Classes define what objects must do and how to do this.
In good design, they implement interfaces.
Classes can be instantiated into objects.
<?php
class Foo implements FooInterface {
public function doFoo($foo) {
return sprintf('Hello %s!', $foo);
}
}
$foo = new Foo();
$foo->doFoo('world');
// returns "Hello world!"
ABSTRACT CLASSES
Abstract classes provide partial implementations, but cannot be
instantiated.
<?php
abstract class AbstractFoo implements FooMultipleInterface {
public function doFoo($foo) {
return sprintf('Hello %s!', $foo);
}
}
class Foo extends AbstractFoo implements FooMultipleInterface {
public function doFooMultiple(array $foos) {
$greetings = [];
foreach ($foos as $foo) {
$greetings[] = sprintf('Hello %s!', $foo);
}
return implode(' ', $greetings);
}
}
TRAITS
Traits provide re-usable implementations that can reduce boilerplate code.
Classes can use traits.
<?php
trait FooTrait {
public function doFoo($foo) {
return sprintf('Hello %s!', $foo);
}
}
class Foo implements FooInterface {
use FooTrait;
}
$foo = new Foo();
$foo->doFoo('world');
// returns "Hello world!"
INHERITANCE
Interfaces, traits, and classes can be extended.
Child classes can access methods from their parent classes.
<?php
class Foo implements FooInterface {
public function doFoo($foo) {
return sprintf('Hello %s!', $foo);
}
}
class PoliteFoo extends Foo {
public function doFoo($foo) {
$message = parent::doFoo($foo);
$message .= ' How are you?';
return $message;
}
}
$THIS
$this points to the current object.
<?php
abstract class AbstractFoo implements FooMultipleInterface {
public function doFoo($foo) {
return sprintf('Hello %s!', $foo);
}
}
class Foo extends AbstractFoo implements FooMultipleInterface {
public function doFooMultiple(array $foos) {
$greetings = [];
foreach ($foos as $foo) {
$greetings[] = $this->doFoo($foo);
}
return implode(' ', $greetings);
}
}
VISIBILITY
Developers control which methods can be called from outside the class.
public
Can be called from anywhere.
protected
Can be called only from within the class or any child class.
private
Can only be called from within the same class.
<?php
class Bar {
protected function doBar() {}
}
$bar = new Bar();
$bar->doBar();
// $bar->doBar() causes an error, because we call a protected method from outside the class.
CHECKING AN OBJECT'S TYPE
When accepted as a function parameter (type hinting).
In a block of code (instanceof).
<?php
function foo(FooInterface $foo) {
// Here we only know $foo implements FooInterface.
if ($foo instanceof FooMultipleInterface) {
// Now we know $foo implements FooMultipleInterface too.
}
}
AUTOLOADING USING PSR-4
Industry standard with several available autoloaders.
Class names map to file names.
Namespaces map to directory names.
Much faster and less frustrating than the Drupal 7 registry.
Example: DrupalpaymentEntityPaymentInterfacemaps to
./src/Entity/PaymentInterface.php.
BENEFITS
Classes objects are faster than arrays
( ).
Interfaces and classes are documentation.
IDEs use interfaces and classes for code completion.
Easier and faster coding. Lower chance of bugs.
https://gist.github.com/nikic/5015323
CONCLUSION
OOP MAKES YOU A BETTER DEVELOPER.
Review this presentation at .http://slideshare.net/bartfeenstra
I am , , and .Xano @BartFeenstra http://mynameisbart.com
DO YOU HAVE ANY QUESTIONS?

More Related Content

What's hot (19)

Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
sandeep54552
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
Sayed Ahmed
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
psnreddy php-oops
psnreddy  php-oopspsnreddy  php-oops
psnreddy php-oops
satya552
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Alena Holligan
 
Php oop presentation
Php   oop presentationPhp   oop presentation
Php oop presentation
Mutinda Boniface
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
prabhat kumar
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
Alena Holligan
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
Abhishek Wadhwa
 
Friend Function
Friend FunctionFriend Function
Friend Function
Mehak Tawakley
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
Mindfire Solutions
 
Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 
Php Oop
Php OopPhp Oop
Php Oop
mussawir20
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
Nyros Technologies
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
sandeep54552
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
Sayed Ahmed
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
psnreddy php-oops
psnreddy  php-oopspsnreddy  php-oops
psnreddy php-oops
satya552
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
Abhishek Wadhwa
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 
Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
Nyros Technologies
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 

Viewers also liked (18)

PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
Bart Feenstra
 
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
Bart Feenstra
 
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
Bart Feenstra
 
Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2
Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2
Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2
benstoraro
 
De matias emergencia policial - pase a retiro - imposibilidad del gobernado...
De matias   emergencia policial - pase a retiro - imposibilidad del gobernado...De matias   emergencia policial - pase a retiro - imposibilidad del gobernado...
De matias emergencia policial - pase a retiro - imposibilidad del gobernado...
Luis Federico Arias
 
Tp6 - Plan de Medios Online
Tp6 - Plan de Medios OnlineTp6 - Plan de Medios Online
Tp6 - Plan de Medios Online
GeatwayViajes
 
CHARLIE PREYER.docx resume
CHARLIE PREYER.docx resumeCHARLIE PREYER.docx resume
CHARLIE PREYER.docx resume
charlie preyer
 
Inversión total y estructura financiera
Inversión total y estructura financieraInversión total y estructura financiera
Inversión total y estructura financiera
Leslie Jeanette Hernandez Garcia
 
Writing news and lead story
Writing news and lead storyWriting news and lead story
Writing news and lead story
mediamaryam
 
Presentazione Giulia Covino
Presentazione Giulia CovinoPresentazione Giulia Covino
Presentazione Giulia Covino
LVenture Group
 
20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...
20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...
20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...
Dale Sternberg
 
UNDERGRADUATE COURSE DESCRIPTIONS
UNDERGRADUATE COURSE DESCRIPTIONSUNDERGRADUATE COURSE DESCRIPTIONS
UNDERGRADUATE COURSE DESCRIPTIONS
Robert Palladino, MA
 
美麗的圖片A
美麗的圖片A美麗的圖片A
美麗的圖片A
燿君 吳
 
Pracmatics
PracmaticsPracmatics
Pracmatics
Lina Maria Avila Rojas
 
Formulir pengajuan Small Grant Activity PWYP Indonesia
Formulir pengajuan Small Grant Activity PWYP  IndonesiaFormulir pengajuan Small Grant Activity PWYP  Indonesia
Formulir pengajuan Small Grant Activity PWYP Indonesia
Publish What You Pay (PWYP) Indonesia
 
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
Bart Feenstra
 
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
Bart Feenstra
 
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
Bart Feenstra
 
Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2
Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2
Unit 73 ig1 assignment computer game audio cut sequence production 2013_y2
benstoraro
 
De matias emergencia policial - pase a retiro - imposibilidad del gobernado...
De matias   emergencia policial - pase a retiro - imposibilidad del gobernado...De matias   emergencia policial - pase a retiro - imposibilidad del gobernado...
De matias emergencia policial - pase a retiro - imposibilidad del gobernado...
Luis Federico Arias
 
Tp6 - Plan de Medios Online
Tp6 - Plan de Medios OnlineTp6 - Plan de Medios Online
Tp6 - Plan de Medios Online
GeatwayViajes
 
CHARLIE PREYER.docx resume
CHARLIE PREYER.docx resumeCHARLIE PREYER.docx resume
CHARLIE PREYER.docx resume
charlie preyer
 
Writing news and lead story
Writing news and lead storyWriting news and lead story
Writing news and lead story
mediamaryam
 
Presentazione Giulia Covino
Presentazione Giulia CovinoPresentazione Giulia Covino
Presentazione Giulia Covino
LVenture Group
 
20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...
20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...
20150319 primemobilerealestatesecuringyourplaceonthelockscreen-150320133619-c...
Dale Sternberg
 
美麗的圖片A
美麗的圖片A美麗的圖片A
美麗的圖片A
燿君 吳
 
Ad

Similar to An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015) (20)

Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
PHP OOP
PHP OOPPHP OOP
PHP OOP
Oscar Merida
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphp
Alena Holligan
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
Sayed Ahmed
 
Oopsinphp
OopsinphpOopsinphp
Oopsinphp
NithyaNithyav
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
Oops
OopsOops
Oops
baabtra.com - No. 1 supplier of quality freshers
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
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
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Henry Osborne
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Tarek Mahmud Apu
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
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
 
OOP
OOPOOP
OOP
thinkphp
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
baabtra.com - No. 1 supplier of quality freshers
 
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
 
Oo ps
Oo psOo ps
Oo ps
seema chauhan
 
Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Alena Holligan
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphp
Alena Holligan
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
Sayed Ahmed
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
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
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
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
 
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
 
Ad

Recently uploaded (17)

Cloud VPS Provider in India: The Best Hosting Solution for Your Business
Cloud VPS Provider in India: The Best Hosting Solution for Your BusinessCloud VPS Provider in India: The Best Hosting Solution for Your Business
Cloud VPS Provider in India: The Best Hosting Solution for Your Business
DanaJohnson510230
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
Networking concepts from zero to hero that covers the security aspects
Networking concepts from zero to hero that covers the security aspectsNetworking concepts from zero to hero that covers the security aspects
Networking concepts from zero to hero that covers the security aspects
amansinght675
 
How to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real TalkHow to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real Talk
Cam Sites Expert
 
AI REPLACING HUMANS /FATHER OF AI/BIRTH OF AI
AI REPLACING HUMANS /FATHER OF AI/BIRTH OF AIAI REPLACING HUMANS /FATHER OF AI/BIRTH OF AI
AI REPLACING HUMANS /FATHER OF AI/BIRTH OF AI
skdav34
 
OSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptxOSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptx
faizanaseem873
 
5 Reasons cheap WordPress hosting is costing you more | Reversed Out
5 Reasons cheap WordPress hosting is costing you more | Reversed Out5 Reasons cheap WordPress hosting is costing you more | Reversed Out
5 Reasons cheap WordPress hosting is costing you more | Reversed Out
Reversed Out Creative
 
all Practical Project LAST summary note.docx
all Practical Project LAST summary note.docxall Practical Project LAST summary note.docx
all Practical Project LAST summary note.docx
seidjemal94
 
Essential Tech Stack for Effective Shopify Dropshipping Integration.pdf
Essential Tech Stack for Effective Shopify Dropshipping Integration.pdfEssential Tech Stack for Effective Shopify Dropshipping Integration.pdf
Essential Tech Stack for Effective Shopify Dropshipping Integration.pdf
CartCoders
 
10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx
EphraimOOghodero
 
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
Taqyea
 
Frontier Unlimited Internet Setup Step-by-Step Guide.pdf
Frontier Unlimited Internet Setup Step-by-Step Guide.pdfFrontier Unlimited Internet Setup Step-by-Step Guide.pdf
Frontier Unlimited Internet Setup Step-by-Step Guide.pdf
Internet Bundle Now
 
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptxTransport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
ssuser80a7e81
 
basic to advance network security concepts
basic to advance network security conceptsbasic to advance network security concepts
basic to advance network security concepts
amansinght675
 
Presentation About The Buttons | Selma SALTIK
Presentation About The Buttons | Selma SALTIKPresentation About The Buttons | Selma SALTIK
Presentation About The Buttons | Selma SALTIK
SELMA SALTIK
 
Networking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptxNetworking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptx
elestirmen
 
HPC_Course_Presentation_No_Images included.pptx
HPC_Course_Presentation_No_Images included.pptxHPC_Course_Presentation_No_Images included.pptx
HPC_Course_Presentation_No_Images included.pptx
naziaahmadnm
 
Cloud VPS Provider in India: The Best Hosting Solution for Your Business
Cloud VPS Provider in India: The Best Hosting Solution for Your BusinessCloud VPS Provider in India: The Best Hosting Solution for Your Business
Cloud VPS Provider in India: The Best Hosting Solution for Your Business
DanaJohnson510230
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
Networking concepts from zero to hero that covers the security aspects
Networking concepts from zero to hero that covers the security aspectsNetworking concepts from zero to hero that covers the security aspects
Networking concepts from zero to hero that covers the security aspects
amansinght675
 
How to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real TalkHow to Make Money as a Cam Model – Tips, Tools & Real Talk
How to Make Money as a Cam Model – Tips, Tools & Real Talk
Cam Sites Expert
 
AI REPLACING HUMANS /FATHER OF AI/BIRTH OF AI
AI REPLACING HUMANS /FATHER OF AI/BIRTH OF AIAI REPLACING HUMANS /FATHER OF AI/BIRTH OF AI
AI REPLACING HUMANS /FATHER OF AI/BIRTH OF AI
skdav34
 
OSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptxOSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptx
faizanaseem873
 
5 Reasons cheap WordPress hosting is costing you more | Reversed Out
5 Reasons cheap WordPress hosting is costing you more | Reversed Out5 Reasons cheap WordPress hosting is costing you more | Reversed Out
5 Reasons cheap WordPress hosting is costing you more | Reversed Out
Reversed Out Creative
 
all Practical Project LAST summary note.docx
all Practical Project LAST summary note.docxall Practical Project LAST summary note.docx
all Practical Project LAST summary note.docx
seidjemal94
 
Essential Tech Stack for Effective Shopify Dropshipping Integration.pdf
Essential Tech Stack for Effective Shopify Dropshipping Integration.pdfEssential Tech Stack for Effective Shopify Dropshipping Integration.pdf
Essential Tech Stack for Effective Shopify Dropshipping Integration.pdf
CartCoders
 
10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx10 Latest Technologies and Their Benefits to End.pptx
10 Latest Technologies and Their Benefits to End.pptx
EphraimOOghodero
 
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
Taqyea
 
Frontier Unlimited Internet Setup Step-by-Step Guide.pdf
Frontier Unlimited Internet Setup Step-by-Step Guide.pdfFrontier Unlimited Internet Setup Step-by-Step Guide.pdf
Frontier Unlimited Internet Setup Step-by-Step Guide.pdf
Internet Bundle Now
 
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptxTransport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
ssuser80a7e81
 
basic to advance network security concepts
basic to advance network security conceptsbasic to advance network security concepts
basic to advance network security concepts
amansinght675
 
Presentation About The Buttons | Selma SALTIK
Presentation About The Buttons | Selma SALTIKPresentation About The Buttons | Selma SALTIK
Presentation About The Buttons | Selma SALTIK
SELMA SALTIK
 
Networking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptxNetworking_Essentials_version_3.0_-_Module_7.pptx
Networking_Essentials_version_3.0_-_Module_7.pptx
elestirmen
 
HPC_Course_Presentation_No_Images included.pptx
HPC_Course_Presentation_No_Images included.pptxHPC_Course_Presentation_No_Images included.pptx
HPC_Course_Presentation_No_Images included.pptx
naziaahmadnm
 

An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

  • 1. AN INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) I am , , and .Xano @BartFeenstra http://mynameisbart.com
  • 2. WHAT IS OBJECT-ORIENTED PROGRAMMING? The form of programming that uses instances (objects) of classes (predefined data types) to structure (group) information and functionality.
  • 3. GLOSSARY Class A predefined type (blueprint) of complex data. Object An instance of a class; a single unit of complex data. Property A variable that belongs to a class or object. Method A function that belongs to a class or object.
  • 4. STATE Objects have state (internal configuration) which can be changed. Most, if not all usages of static variables in Drupal 7 have been replaced with object properties in Drupal 8.
  • 5. INTERFACES Interfaces define what objects must be able to do, but do not define how they must do these things. They are contracts that must be fulfilled. <?php interface FooInterface { public function doFoo($foo); } interface FooMultipleInterface extends FooInterface { public function doFooMultiple(array $foos); }
  • 6. CLASSES Classes define what objects must do and how to do this. In good design, they implement interfaces. Classes can be instantiated into objects. <?php class Foo implements FooInterface { public function doFoo($foo) { return sprintf('Hello %s!', $foo); } } $foo = new Foo(); $foo->doFoo('world'); // returns "Hello world!"
  • 7. ABSTRACT CLASSES Abstract classes provide partial implementations, but cannot be instantiated. <?php abstract class AbstractFoo implements FooMultipleInterface { public function doFoo($foo) { return sprintf('Hello %s!', $foo); } } class Foo extends AbstractFoo implements FooMultipleInterface { public function doFooMultiple(array $foos) { $greetings = []; foreach ($foos as $foo) { $greetings[] = sprintf('Hello %s!', $foo); } return implode(' ', $greetings); } }
  • 8. TRAITS Traits provide re-usable implementations that can reduce boilerplate code. Classes can use traits. <?php trait FooTrait { public function doFoo($foo) { return sprintf('Hello %s!', $foo); } } class Foo implements FooInterface { use FooTrait; } $foo = new Foo(); $foo->doFoo('world'); // returns "Hello world!"
  • 9. INHERITANCE Interfaces, traits, and classes can be extended. Child classes can access methods from their parent classes. <?php class Foo implements FooInterface { public function doFoo($foo) { return sprintf('Hello %s!', $foo); } } class PoliteFoo extends Foo { public function doFoo($foo) { $message = parent::doFoo($foo); $message .= ' How are you?'; return $message; } }
  • 10. $THIS $this points to the current object. <?php abstract class AbstractFoo implements FooMultipleInterface { public function doFoo($foo) { return sprintf('Hello %s!', $foo); } } class Foo extends AbstractFoo implements FooMultipleInterface { public function doFooMultiple(array $foos) { $greetings = []; foreach ($foos as $foo) { $greetings[] = $this->doFoo($foo); } return implode(' ', $greetings); } }
  • 11. VISIBILITY Developers control which methods can be called from outside the class. public Can be called from anywhere. protected Can be called only from within the class or any child class. private Can only be called from within the same class. <?php class Bar { protected function doBar() {} } $bar = new Bar(); $bar->doBar(); // $bar->doBar() causes an error, because we call a protected method from outside the class.
  • 12. CHECKING AN OBJECT'S TYPE When accepted as a function parameter (type hinting). In a block of code (instanceof). <?php function foo(FooInterface $foo) { // Here we only know $foo implements FooInterface. if ($foo instanceof FooMultipleInterface) { // Now we know $foo implements FooMultipleInterface too. } }
  • 13. AUTOLOADING USING PSR-4 Industry standard with several available autoloaders. Class names map to file names. Namespaces map to directory names. Much faster and less frustrating than the Drupal 7 registry. Example: DrupalpaymentEntityPaymentInterfacemaps to ./src/Entity/PaymentInterface.php.
  • 14. BENEFITS Classes objects are faster than arrays ( ). Interfaces and classes are documentation. IDEs use interfaces and classes for code completion. Easier and faster coding. Lower chance of bugs. https://gist.github.com/nikic/5015323
  • 15. CONCLUSION OOP MAKES YOU A BETTER DEVELOPER. Review this presentation at .http://slideshare.net/bartfeenstra I am , , and .Xano @BartFeenstra http://mynameisbart.com DO YOU HAVE ANY QUESTIONS?