Object Oriented Programming

Summary

  • Visibility of methods/properties (Encapsulation)
  • Single Responsibility
  • Design by Contract (PSR)
  • Composition over inheritance
  • Dependency Injection
  • Law of Demeter
  • Temporal Coupling
  • Immutability
  • Exceptions 

Single Responsibility

A class should have only one reason to change.

(Robert C. Martin)

<?php
class Book {
 
    function getTitle() {
        return "A Great Book";
    }
 
    function getAuthor() {
        return "John Doe";
    }
 
    function turnPage() {
        // pointer to next page
    }
 
    function printCurrentPage() {
        echo "current page content";
    }
}
class Book
{
    function getTitle()
    {
        return "A Great Book";
    }
 
    function getAuthor()
    {
        return "John Doe";
    }
 
    function turnPage()
    {
        // pointer to next page
    }
 
    function getCurrentPage()
    {
        return "current page content";
    }
 
}
interface Printer
{
    function printPage($page);
}
 
class PlainTextPrinter implements Printer
{
    function printPage($page)
    {
        echo $page;
    }
 
}
 
class HtmlPrinter implements Printer
{
    function printPage($page)
    {
        echo '<div style="single-page">'
            . $page
            . '</div>';
    }
}

Composition over inheritance

Visibility of methods/properties

Dependency Injection

Law of Demeter

Temporal Coupling

Immutability

Design by Contract

Final classes

Exceptions

Object Oriented Programming

By Nicolas Eeckeloo

Object Oriented Programming

  • 608