An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
Click here to load reader
-
Upload
bart-feenstra -
Category
Internet
-
view
342 -
download
0
Transcript of An Introduction to Object-Oriented Programming (DrupalCamp Leuven 2015)
AN INTRODUCTION TOOBJECT-ORIENTED
PROGRAMMING (OOP)I am , , and .Xano @BartFeenstra http://mynameisbart.com
WHAT IS OBJECT-ORIENTEDPROGRAMMING?
The form of programming that uses instances (objects) of classes(predefined data types) to structure (group) information and functionality.
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.
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.
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);
}
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!"
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); }}
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!"
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; }
}
$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); }}
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.
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. }}
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.
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
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?