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

PDF
An Introduction to Object-Oriented Programming (SaunaCamp Helsinki 2015)
PDF
An Introduction to Object-Oriented Programming (DrupalCamp London 2015)
PDF
Obect-Oriented Collaboration
PPT
Class 7 - PHP Object Oriented Programming
PPT
Class and Objects in PHP
PPTX
Object oriented programming in php
PDF
Object Oriented Programming with PHP 5 - More OOP
An Introduction to Object-Oriented Programming (SaunaCamp Helsinki 2015)
An Introduction to Object-Oriented Programming (DrupalCamp London 2015)
Obect-Oriented Collaboration
Class 7 - PHP Object Oriented Programming
Class and Objects in PHP
Object oriented programming in php
Object Oriented Programming with PHP 5 - More OOP

What's hot (19)

PDF
Chapter23 friend-function-friend-class
PPTX
C++ programming introduction
PPTX
Object oriented programming in php 5
PPTX
Inheritance, friend function, virtual function, polymorphism
PDF
psnreddy php-oops
PDF
OOP in PHP
PPTX
Php oop presentation
PPTX
Polymorphism
PDF
Demystifying oop
PPTX
Friend function & friend class
PPTX
Friend Function
PPTX
Oop in-php
PPT
Oops in PHP
PPTX
Friend functions
PPT
Php Oop
PDF
Object Oriented Programming in PHP
PPTX
Class and object
PPT
Oops in PHP By Nyros Developer
PPT
Friends function and_classes
Chapter23 friend-function-friend-class
C++ programming introduction
Object oriented programming in php 5
Inheritance, friend function, virtual function, polymorphism
psnreddy php-oops
OOP in PHP
Php oop presentation
Polymorphism
Demystifying oop
Friend function & friend class
Friend Function
Oop in-php
Oops in PHP
Friend functions
Php Oop
Object Oriented Programming in PHP
Class and object
Oops in PHP By Nyros Developer
Friends function and_classes
Ad

Viewers also liked (20)

PDF
EuroPython 2015 - Decorators demystified
PDF
The (unknown) collections module
PDF
Oop concepts classes_objects
PDF
Meta-Classes in Python
PDF
Message-passing concurrency in Python
PDF
Python decorators
PPTX
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
PPTX
Python decorators
PDF
Multiprocessing with python
PDF
Interfacing C/C++ and Python with SWIG
PDF
Python in Action (Part 1)
PPTX
Prepping the Analytics organization for Artificial Intelligence evolution
PDF
Mastering Python 3 I/O (Version 2)
PDF
An Introduction to Python Concurrency
PDF
QCon Rio - Machine Learning for Everyone
PDF
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
PPTX
Tutorial on Deep learning and Applications
PDF
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
PDF
Hands-on Deep Learning in Python
PDF
Deep Learning - The Past, Present and Future of Artificial Intelligence
EuroPython 2015 - Decorators demystified
The (unknown) collections module
Oop concepts classes_objects
Meta-Classes in Python
Message-passing concurrency in Python
Python decorators
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Python decorators
Multiprocessing with python
Interfacing C/C++ and Python with SWIG
Python in Action (Part 1)
Prepping the Analytics organization for Artificial Intelligence evolution
Mastering Python 3 I/O (Version 2)
An Introduction to Python Concurrency
QCon Rio - Machine Learning for Everyone
Python for Image Understanding: Deep Learning with Convolutional Neural Nets
Tutorial on Deep learning and Applications
An Introduction to Supervised Machine Learning and Pattern Classification: Th...
Hands-on Deep Learning in Python
Deep Learning - The Past, Present and Future of Artificial Intelligence
Ad

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

PDF
Object_oriented_programming_OOP_with_PHP.pdf
PDF
PHP OOP
PDF
Demystifying Object-Oriented Programming - PHP UK Conference 2017
PDF
Take the Plunge with OOP from #pnwphp
PPT
Introduction to OOP with PHP
PPTX
Object oriented programming in php 5
PPTX
Oopsinphp
PDF
Demystifying Object-Oriented Programming #ssphp16
PPTX
OOPS Characteristics (With Examples in PHP)
PDF
Demystifying Object-Oriented Programming #phpbnl18
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
PPTX
OOP in PHP
PDF
OOP in PHP
PPTX
Lecture-10_PHP-OOP.pptx
PDF
Demystifying Object-Oriented Programming - ZendCon 2016
PPT
PPTX
Coming to Terms with OOP In Drupal - php[world] 2016
PPTX
Object_oriented_programming_OOP_with_PHP.pdf
PHP OOP
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Take the Plunge with OOP from #pnwphp
Introduction to OOP with PHP
Object oriented programming in php 5
Oopsinphp
Demystifying Object-Oriented Programming #ssphp16
OOPS Characteristics (With Examples in PHP)
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming - Lone Star PHP
OOP in PHP
OOP in PHP
Lecture-10_PHP-OOP.pptx
Demystifying Object-Oriented Programming - ZendCon 2016
Coming to Terms with OOP In Drupal - php[world] 2016

More from Bart Feenstra (7)

PDF
PHP-FIG: how the PHP world got off their islands (DrupalCamp Vienna 2015)
PDF
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 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)
An Introduction to Object-Oriented Programming (DrupalCamp Leuven 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
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
DOCX
Unit-3 cyber security network security of internet system
PDF
Introduction to the IoT system, how the IoT system works
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PPT
Ethics in Information System - Management Information System
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
Internet___Basics___Styled_ presentation
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
artificial intelligence overview of it and more
PPT
tcp ip networks nd ip layering assotred slides
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
innovation process that make everything different.pptx
Cloud-Scale Log Monitoring _ Datadog.pdf
INTERNET------BASICS-------UPDATED PPT PRESENTATION
introduction about ICD -10 & ICD-11 ppt.pptx
Module 1 - Cyber Law and Ethics 101.pptx
Unit-3 cyber security network security of internet system
Introduction to the IoT system, how the IoT system works
Power Point - Lesson 3_2.pptx grad school presentation
Ethics in Information System - Management Information System
Introuction about ICD -10 and ICD-11 PPT.pptx
Internet___Basics___Styled_ presentation
international classification of diseases ICD-10 review PPT.pptx
artificial intelligence overview of it and more
tcp ip networks nd ip layering assotred slides
The New Creative Director: How AI Tools for Social Media Content Creation Are...
SAP Ariba Sourcing PPT for learning material
WebRTC in SignalWire - troubleshooting media negotiation
Decoding a Decade: 10 Years of Applied CTI Discipline
artificialintelligenceai1-copy-210604123353.pptx
Unit-1 introduction to cyber security discuss about how to secure a system
innovation process that make everything different.pptx

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?