SlideShare a Scribd company logo
1 of 15
Download to read offline
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

What's hot

Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in php
Aashiq Kuchey
 
Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 

What's hot (20)

C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in php
 
Php oop presentation
Php   oop presentationPhp   oop presentation
Php oop presentation
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Friend functions
Friend functions Friend functions
Friend functions
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Inheritance
InheritanceInheritance
Inheritance
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
 
Php Oop
Php OopPhp Oop
Php Oop
 
C plusplus
C plusplusC plusplus
C plusplus
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 

Viewers also liked

Viewers also liked (16)

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

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

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

OOP
OOPOOP
OOP
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
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
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
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
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
php_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdfphp_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdf
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_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.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
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
 
Design attern in php
Design attern in phpDesign attern in php
Design attern in php
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
 
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
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
PHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptxPHP OOP Lecture - 04.pptx
PHP OOP Lecture - 04.pptx
 

More from Bart Feenstra

More from Bart Feenstra (6)

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

Recently uploaded

Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
imonikaupta
 

Recently uploaded (20)

Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceReal Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 

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?