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 (20)

EuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystifiedEuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystified
Pablo Enfedaque
 
The (unknown) collections module
The (unknown) collections moduleThe (unknown) collections module
The (unknown) collections module
Pablo Enfedaque
 
Oop concepts classes_objects
Oop concepts classes_objectsOop concepts classes_objects
Oop concepts classes_objects
William Olivier
 
Meta-Classes in Python
Meta-Classes in PythonMeta-Classes in Python
Meta-Classes in Python
Guy Wiener
 
Message-passing concurrency in Python
Message-passing concurrency in PythonMessage-passing concurrency in Python
Message-passing concurrency in Python
Sarah Mount
 
Python decorators
Python decoratorsPython decorators
Python decorators
Guillermo Blasco Jiménez
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
Python decorators
Python decoratorsPython decorators
Python decorators
Alex Su
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with python
Patrick Vergain
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
David Beazley (Dabeaz LLC)
 
Python in Action (Part 1)
Python in Action (Part 1)Python in Action (Part 1)
Python in Action (Part 1)
David Beazley (Dabeaz LLC)
 
Prepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolutionPrepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolution
Ramkumar Ravichandran
 
Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)Mastering Python 3 I/O (Version 2)
Mastering Python 3 I/O (Version 2)
David Beazley (Dabeaz LLC)
 
An Introduction to Python Concurrency
An Introduction to Python ConcurrencyAn Introduction to Python Concurrency
An Introduction to Python Concurrency
David Beazley (Dabeaz LLC)
 
QCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for EveryoneQCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for Everyone
Dhiana Deva
 
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural NetsPython for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Roelof Pieters
 
Tutorial on Deep learning and Applications
Tutorial on Deep learning and ApplicationsTutorial on Deep learning and Applications
Tutorial on Deep learning and Applications
NhatHai Phan
 
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
An Introduction to Supervised Machine Learning and Pattern Classification: Th...An Introduction to Supervised Machine Learning and Pattern Classification: Th...
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
Sebastian Raschka
 
Hands-on Deep Learning in Python
Hands-on Deep Learning in PythonHands-on Deep Learning in Python
Hands-on Deep Learning in Python
Imry Kissos
 
Deep Learning - The Past, Present and Future of Artificial Intelligence
Deep Learning - The Past, Present and Future of Artificial IntelligenceDeep Learning - The Past, Present and Future of Artificial Intelligence
Deep Learning - The Past, Present and Future of Artificial Intelligence
Lukas Masuch
 
EuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystifiedEuroPython 2015 - Decorators demystified
EuroPython 2015 - Decorators demystified
Pablo Enfedaque
 
The (unknown) collections module
The (unknown) collections moduleThe (unknown) collections module
The (unknown) collections module
Pablo Enfedaque
 
Oop concepts classes_objects
Oop concepts classes_objectsOop concepts classes_objects
Oop concepts classes_objects
William Olivier
 
Meta-Classes in Python
Meta-Classes in PythonMeta-Classes in Python
Meta-Classes in Python
Guy Wiener
 
Message-passing concurrency in Python
Message-passing concurrency in PythonMessage-passing concurrency in Python
Message-passing concurrency in Python
Sarah Mount
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
Python decorators
Python decoratorsPython decorators
Python decorators
Alex Su
 
Multiprocessing with python
Multiprocessing with pythonMultiprocessing with python
Multiprocessing with python
Patrick Vergain
 
Prepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolutionPrepping the Analytics organization for Artificial Intelligence evolution
Prepping the Analytics organization for Artificial Intelligence evolution
Ramkumar Ravichandran
 
QCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for EveryoneQCon Rio - Machine Learning for Everyone
QCon Rio - Machine Learning for Everyone
Dhiana Deva
 
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural NetsPython for Image Understanding: Deep Learning with Convolutional Neural Nets
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Roelof Pieters
 
Tutorial on Deep learning and Applications
Tutorial on Deep learning and ApplicationsTutorial on Deep learning and Applications
Tutorial on Deep learning and Applications
NhatHai Phan
 
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
An Introduction to Supervised Machine Learning and Pattern Classification: Th...An Introduction to Supervised Machine Learning and Pattern Classification: Th...
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
Sebastian Raschka
 
Hands-on Deep Learning in Python
Hands-on Deep Learning in PythonHands-on Deep Learning in Python
Hands-on Deep Learning in Python
Imry Kissos
 
Deep Learning - The Past, Present and Future of Artificial Intelligence
Deep Learning - The Past, Present and Future of Artificial IntelligenceDeep Learning - The Past, Present and Future of Artificial Intelligence
Deep Learning - The Past, Present and Future of Artificial Intelligence
Lukas Masuch
 
Ad

Similar to An Introduction to Object-Oriented Programming (DrupalCamp North 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

More from Bart Feenstra (7)

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
 
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 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
 
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
The Drupal 8 plugin system: extensibility for allThe Drupal 8 plugin system: extensibility for all
The Drupal 8 plugin system: extensibility for all
Bart Feenstra
 
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
Entity API in Drupal 8 (Drupal Tech Talk October 2014)Entity API in Drupal 8 (Drupal Tech Talk October 2014)
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
Bart Feenstra
 
Payment processing in drupal 8 (DrupalCamp Ghent 2014)
Payment processing in drupal 8 (DrupalCamp Ghent 2014)Payment processing in drupal 8 (DrupalCamp Ghent 2014)
Payment processing in drupal 8 (DrupalCamp Ghent 2014)
Bart Feenstra
 
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
 
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 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
 
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
The Drupal 8 plugin system: extensibility for allThe Drupal 8 plugin system: extensibility for all
The Drupal 8 plugin system: extensibility for all
Bart Feenstra
 
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
Entity API in Drupal 8 (Drupal Tech Talk October 2014)Entity API in Drupal 8 (Drupal Tech Talk October 2014)
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
Bart Feenstra
 
Payment processing in drupal 8 (DrupalCamp Ghent 2014)
Payment processing in drupal 8 (DrupalCamp Ghent 2014)Payment processing in drupal 8 (DrupalCamp Ghent 2014)
Payment processing in drupal 8 (DrupalCamp Ghent 2014)
Bart Feenstra
 

Recently uploaded (17)

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
 
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
 
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
 
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
 
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
 
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 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
 
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
 
basic to advance network security concepts
basic to advance network security conceptsbasic to advance network security concepts
basic to advance network security concepts
amansinght675
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
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
 
OSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptxOSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptx
faizanaseem873
 
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptxTransport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
ssuser80a7e81
 
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
 
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
 
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
Taqyea
 
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
 
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
 
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
 
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
 
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
 
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
 
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 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
 
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
 
basic to advance network security concepts
basic to advance network security conceptsbasic to advance network security concepts
basic to advance network security concepts
amansinght675
 
ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676ARTIFICIAL INTELLIGENCE.pptx2565567765676
ARTIFICIAL INTELLIGENCE.pptx2565567765676
areebaimtiazpmas
 
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
 
OSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptxOSI_Security_Architecture Computer Science.pptx
OSI_Security_Architecture Computer Science.pptx
faizanaseem873
 
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptxTransport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
Transport Conjjjjjjjjjjjjjjjjjjjjjjjsulting by Slidesgo.pptx
ssuser80a7e81
 
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
 
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
 
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
原版西班牙马拉加大学毕业证(UMA毕业证书)如何办理
Taqyea
 
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 North 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?