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

15

Click here to load reader

Transcript of An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

Page 1: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

AN INTRODUCTION TOOBJECT-ORIENTED

PROGRAMMING (OOP)I am , , and .Xano @BartFeenstra http://mynameisbart.com

Page 2: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

WHAT IS OBJECT-ORIENTEDPROGRAMMING?

The form of programming that uses instances (objects) of classes(predefined data types) to structure (group) information and functionality.

Page 3: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

GLOSSARYClass

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.

Page 4: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

STATEObjects have state (internal configuration) which can be changed.

Most, if not all usages of static variables in Drupal 7 have been replaced withobject properties in Drupal 8.

Page 5: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

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

}

Page 6: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

CLASSESClasses 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!"

Page 7: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

ABSTRACT CLASSESAbstract 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); }}

Page 8: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

TRAITSTraits 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!"

Page 9: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

INHERITANCEInterfaces, 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; }

}

Page 10: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

$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); }}

Page 11: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

VISIBILITYDevelopers control which methods can be called from outside the class.

publicCan be called from anywhere.

protectedCan be called only from within the class or any child class.

privateCan 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.

Page 12: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

CHECKING AN OBJECT'S TYPEWhen 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. }}

Page 13: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

AUTOLOADING USING PSR-4Industry 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: \Drupal\payment\Entity\PaymentInterface maps to./src/Entity/PaymentInterface.php.

Page 14: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

BENEFITSClasses 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

Page 15: An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)

CONCLUSIONOOP 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?