SlideShare a Scribd company logo
AN INTRODUCTION TO
OBJECT-ORIENTED
PROGRAMMING (OOP)
Saunacamp Helsinki 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);
}
}
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.
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 (DrupalCamp London 2015)
PDF
Obect-Oriented Collaboration
PPTX
Inheritance, friend function, virtual function, polymorphism
PPT
Inheritance, polymorphisam, abstract classes and composition)
PPT
Class 7 - PHP Object Oriented Programming
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
An Introduction to Object-Oriented Programming (DrupalCamp North 2015)
An Introduction to Object-Oriented Programming (DrupalCamp London 2015)
Obect-Oriented Collaboration
Inheritance, friend function, virtual function, polymorphism
Inheritance, polymorphisam, abstract classes and composition)
Class 7 - PHP Object Oriented Programming

What's hot (20)

PPTX
C++ programming introduction
PDF
Chapter23 friend-function-friend-class
PPTX
Object oriented programming in php
PPTX
Php oop presentation
PPT
Class and Objects in PHP
PPTX
Object oriented programming in php 5
PPTX
Friend function & friend class
PPTX
Friend functions
PPTX
Polymorphism
PDF
Demystifying oop
PPTX
Friend Function
PDF
OOP in PHP
PDF
Object Oriented Programming with PHP 5 - More OOP
PPT
Friends function and_classes
PPTX
Inheritance
PPTX
Oop in-php
PPT
Php Oop
PPT
C plusplus
PPTX
inheritance in C++
PPT
Oops in PHP By Nyros Developer
C++ programming introduction
Chapter23 friend-function-friend-class
Object oriented programming in php
Php oop presentation
Class and Objects in PHP
Object oriented programming in php 5
Friend function & friend class
Friend functions
Polymorphism
Demystifying oop
Friend Function
OOP in PHP
Object Oriented Programming with PHP 5 - More OOP
Friends function and_classes
Inheritance
Oop in-php
Php Oop
C plusplus
inheritance in C++
Oops in PHP By Nyros Developer
Ad

Viewers also liked (16)

PPTX
ISR Systems Development
PPTX
Books That Shaped America - The Snowy Day, by Ezra Jack Keats
PPT
Rosa ariduz
DOCX
Uts psikologi upi pjkr fpok
PPTX
7INNOVA Lesson 5 slides for T26
DOCX
Motor trifásico tipo 1 la7 111
PPTX
Teacher librarianship
PPT
FIRM Sponsorship package
PPT
Sponsorship Package from the FIRM Music Fest
DOCX
Proposal 1
PDF
The Blaze Room Recording Studio presentation
PDF
Condensed fs2015 mar
PDF
SKuehn_MachineLearningAndOptimization_2015
PPTX
Who will be the audience for your media product?
PDF
TYPO3 monitoring with EXT:t3monitoring
PDF
Yamma Brown Biography
ISR Systems Development
Books That Shaped America - The Snowy Day, by Ezra Jack Keats
Rosa ariduz
Uts psikologi upi pjkr fpok
7INNOVA Lesson 5 slides for T26
Motor trifásico tipo 1 la7 111
Teacher librarianship
FIRM Sponsorship package
Sponsorship Package from the FIRM Music Fest
Proposal 1
The Blaze Room Recording Studio presentation
Condensed fs2015 mar
SKuehn_MachineLearningAndOptimization_2015
Who will be the audience for your media product?
TYPO3 monitoring with EXT:t3monitoring
Yamma Brown Biography
Ad

Similar to An Introduction to Object-Oriented Programming (SaunaCamp Helsinki 2015) (20)

PPT
PDF
Demystifying Object-Oriented Programming - ZendCon 2016
PDF
Objects, Testing, and Responsibility
PDF
Take the Plunge with OOP from #pnwphp
PDF
OOP in PHP
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
ZIP
Object Oriented PHP5
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision.pdf
PDF
php_final_sy_semIV_notes_vision (3).pdf
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
PDF
Design attern in php
PDF
Demystifying Object-Oriented Programming #phpbnl18
PPTX
Coming to Terms with OOP In Drupal - php[world] 2016
PPTX
Ch8(oop)
PPTX
PHP OOP Lecture - 04.pptx
Demystifying Object-Oriented Programming - ZendCon 2016
Objects, Testing, and Responsibility
Take the Plunge with OOP from #pnwphp
OOP in PHP
Demystifying Object-Oriented Programming - Lone Star PHP
Object Oriented PHP5
UNIT III (8).pptx
UNIT III (8).pptx
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision (3).pdf
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Design attern in php
Demystifying Object-Oriented Programming #phpbnl18
Coming to Terms with OOP In Drupal - php[world] 2016
Ch8(oop)
PHP OOP Lecture - 04.pptx

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)

DOCX
Unit-3 cyber security network security of internet system
PPTX
newyork.pptxirantrafgshenepalchinachinane
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPT
tcp ip networks nd ip layering assotred slides
PPTX
artificial intelligence overview of it and more
PPTX
E -tech empowerment technologies PowerPoint
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
Mathew Digital SEO Checklist Guidlines 2025
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PPTX
innovation process that make everything different.pptx
PDF
Introduction to the IoT system, how the IoT system works
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPT
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
presentation_pfe-universite-molay-seltan.pptx
Unit-3 cyber security network security of internet system
newyork.pptxirantrafgshenepalchinachinane
Slides PDF The World Game (s) Eco Economic Epochs.pdf
tcp ip networks nd ip layering assotred slides
artificial intelligence overview of it and more
E -tech empowerment technologies PowerPoint
Unit-1 introduction to cyber security discuss about how to secure a system
Mathew Digital SEO Checklist Guidlines 2025
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Paper PDF World Game (s) Great Redesign.pdf
An introduction to the IFRS (ISSB) Stndards.pdf
innovation process that make everything different.pptx
Introduction to the IoT system, how the IoT system works
introduction about ICD -10 & ICD-11 ppt.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps
Design_with_Watersergyerge45hrbgre4top (1).ppt
The New Creative Director: How AI Tools for Social Media Content Creation Are...
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
WebRTC in SignalWire - troubleshooting media negotiation
presentation_pfe-universite-molay-seltan.pptx

An Introduction to Object-Oriented Programming (SaunaCamp Helsinki 2015)

  • 1. AN INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) Saunacamp Helsinki 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); } }
  • 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. Find this presentation at .https://github.com/bartfeenstra/presentation_intro_oop Find me at and .@BartFeenstra http://mynameisbart.com DO YOU HAVE ANY QUESTIONS?