SlideShare a Scribd company logo
AN INTRODUCTION TO
OBJECT-ORIENTED
PROGRAMMING (OOP)
DrupalCamp London 2015
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);
}
}
$foo = new Foo();
$foo->doFooMultiple(['world', 'Drupal']);
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;
}
}
$foo = new Foo();
$foo->doFoo('world');
$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);
}
}
$foo = new Foo();
$foo->doFooMultiple(['world', 'Drupal']);
// returns "Hello world!" and "Hello Drupal!"
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.
Find this presentation at
.https://github.com/bartfeenstra/presentation_intro_oop
Find me at and .@BartFeenstra http://mynameisbart.com
DO YOU HAVE ANY QUESTIONS?

More Related Content

PDF
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
PDF
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
PDF
An Introduction to Object-Oriented Programming (SaunaCamp Helsinki 2015)
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
PDF
Demystifying Object-Oriented Programming - Midwest PHP
PPTX
Ch8(oop)
PDF
Architecture logicielle #3 : object oriented design
PDF
psnreddy php-oops
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
An Introduction to Object-Oriented Programming (SaunaCamp Helsinki 2015)
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Midwest PHP
Ch8(oop)
Architecture logicielle #3 : object oriented design
psnreddy php-oops

What's hot (20)

PDF
Functions in PHP
PDF
Task 2
PDF
Practice exam php
DOCX
Smarty 3 overview
PDF
Delegate
PPTX
Js types
PPTX
PHP Powerpoint -- Teach PHP with this
PDF
OSDC.TW - Gutscript for PHP haters
PDF
The Ring programming language version 1.7 book - Part 35 of 196
PPTX
Python Course for Beginners
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PDF
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
PDF
Sorting arrays in PHP
PPT
pointer, structure ,union and intro to file handling
PPT
PPTX
Hiphop php
PDF
How to write code you won't hate tomorrow
PPTX
Perl names values and variables
PDF
Dependency injection in Drupal 8
Functions in PHP
Task 2
Practice exam php
Smarty 3 overview
Delegate
Js types
PHP Powerpoint -- Teach PHP with this
OSDC.TW - Gutscript for PHP haters
The Ring programming language version 1.7 book - Part 35 of 196
Python Course for Beginners
PHP - DataType,Variable,Constant,Operators,Array,Include and require
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
Sorting arrays in PHP
pointer, structure ,union and intro to file handling
Hiphop php
How to write code you won't hate tomorrow
Perl names values and variables
Dependency injection in Drupal 8
Ad

Viewers also liked (8)

PDF
Object Oriented Programming in JavaScript
PDF
FP 301 OOP FINAL PAPER
DOCX
OBJECT ORIENTED ROGRAMMING With Question And Answer Full
PDF
FP305 data structure PAPER FINAL SEM 3
PDF
FP 301 OOP FINAL PAPER JUNE 2013
PDF
PDF
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMING
PPT
Data structures
Object Oriented Programming in JavaScript
FP 301 OOP FINAL PAPER
OBJECT ORIENTED ROGRAMMING With Question And Answer Full
FP305 data structure PAPER FINAL SEM 3
FP 301 OOP FINAL PAPER JUNE 2013
FINAL PAPER FP301 OBJECT ORIENTED PROGRAMMING
Data structures
Ad

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

PDF
Object_oriented_programming_OOP_with_PHP.pdf
PPT
Class 7 - PHP Object Oriented Programming
PPT
Introduction to OOP with PHP
PPTX
OOPS Characteristics (With Examples in PHP)
PDF
PHP OOP
PDF
OOP in PHP
PPTX
Oopsinphp
PPTX
Lecture-10_PHP-OOP.pptx
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PPT
PPTX
Object oriented programming in php 5
PPTX
Object oriented programming in php 5
PDF
Take the Plunge with OOP from #pnwphp
PDF
Demystifying Object-Oriented Programming #ssphp16
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PDF
Object Oriented Programming in PHP
Object_oriented_programming_OOP_with_PHP.pdf
Class 7 - PHP Object Oriented Programming
Introduction to OOP with PHP
OOPS Characteristics (With Examples in PHP)
PHP OOP
OOP in PHP
Oopsinphp
Lecture-10_PHP-OOP.pptx
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT III (8).pptx
UNIT III (8).pptx
Object oriented programming in php 5
Object oriented programming in php 5
Take the Plunge with OOP from #pnwphp
Demystifying Object-Oriented Programming #ssphp16
09 Object Oriented Programming in PHP #burningkeyboards
Object Oriented Programming in PHP

More from Bart Feenstra (6)

PDF
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
PDF
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
PDF
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
PDF
The Drupal 8 plugin system: extensibility for all
PDF
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
PDF
Payment processing in drupal 8 (DrupalCamp Ghent 2014)
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
The Drupal 8 plugin system: extensibility for all (DrupalCamp Baltics 2015)
The Drupal 8 plugin system: extensibility for all (Drupalaton 2015)
The Drupal 8 plugin system: extensibility for all
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
Payment processing in drupal 8 (DrupalCamp Ghent 2014)

Recently uploaded (20)

PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
Introduction to the IoT system, how the IoT system works
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
SAP Ariba Sourcing PPT for learning material
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPT
Ethics in Information System - Management Information System
PPTX
artificial intelligence overview of it and more
PPTX
Internet___Basics___Styled_ presentation
PPT
tcp ip networks nd ip layering assotred slides
PDF
Sims 4 Historia para lo sims 4 para jugar
PPT
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Introduction to the IoT system, how the IoT system works
Module 1 - Cyber Law and Ethics 101.pptx
SAP Ariba Sourcing PPT for learning material
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Cloud-Scale Log Monitoring _ Datadog.pdf
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Introuction about WHO-FIC in ICD-10.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
presentation_pfe-universite-molay-seltan.pptx
522797556-Unit-2-Temperature-measurement-1-1.pptx
Ethics in Information System - Management Information System
artificial intelligence overview of it and more
Internet___Basics___Styled_ presentation
tcp ip networks nd ip layering assotred slides
Sims 4 Historia para lo sims 4 para jugar
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...

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

  • 1. AN INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) DrupalCamp London 2015
  • 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); } } $foo = new Foo(); $foo->doFooMultiple(['world', 'Drupal']);
  • 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; } } $foo = new Foo(); $foo->doFoo('world');
  • 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); } } $foo = new Foo(); $foo->doFooMultiple(['world', 'Drupal']); // returns "Hello world!" and "Hello Drupal!"
  • 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. Find this presentation at .https://github.com/bartfeenstra/presentation_intro_oop Find me at and .@BartFeenstra http://mynameisbart.com DO YOU HAVE ANY QUESTIONS?