If most of your web development work focuses around current stable versions of Drupal, you may be new to object-oriented programming. This is an approach to coding that centers around objects, which are data of their own types (classes) with their own behavior that are defined by developers and can easily be built upon by other developers. It also includes concepts such as interfaces, more compartmentalized design, dependency injection and unit testing.
This session will explain these concepts to you from the point of view of someone coming from procedural programming, which was Drupal’s predominant coding style before version 8. Afterwards, you will be familiar with the main concepts used by Drupal 8 and other modern PHP libraries and frameworks and you will be able to recognize and work with them in your favorite new Drupal version.
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?