Skip to content

Object Oriented Programming

Marie-Louise edited this page Oct 11, 2018 · 12 revisions

languages like java are purely OOP

PHP is a scripting language that has a bit of HTML, OOP and logic

Basic OOP

Drupal started at a time when object oriented programming ...

4 basic Principles of OOP

  1. Polymorphism
  2. Abstraction
  3. Encapsulation
  4. Inheritance

Glossary

Class - An encapsulation of some functionality

Subclass - An extension to a base class that inherits the base classy functionality but adds more

Property - A property of a class that represents a value

Function - Some logic that either modifies a property or returns some value

Exercise

Create shape classes in PHP

<?php

//base class
class Shape {

declare the base class. Other classes can be declared within this with different values

const SHAPE_TYPE = 1;

protected means that the value cannot be changed

private means that the value can be changed but it can't be accessed directly.

interface is an abstract class....

Class

In an object oriented programming a class is like a template or/ boiler plate for an object. An object in an instance of a class. The class doesn't exist until the an instance or the object is created.

So for example

peter's synth keyboard

In this synth keyboard app developed by Peter, if I were to explain it's construction using classes then:

The base class or parent class would be the keys.

The class instances or objects would be the different types of keys, so the properties would have arguments for the position, pitch/sound etc

OOP is much cleaner and maintainable way of programming. 60 years ago, only functions were written to perform tasks but now this new structure has improved the efficiency of programming.

Examples of how to refactor classes

Single Responsibility Principle Violation

<?php

class Report
{
    public function getTitle()
    {
        return 'Report Title';
    }
    public function getDate()
    {
    return '2016-04-21';
    }
    public function getContents()
    {
        return [
            'title' => $this->getTitle(),
            'date' => $this->getDate(),
        ];
    }
    public function formatJson()
    {
        return json_encode($this->getContents());
    }
}

here you have the class Report which has the property declarations (in this case all four are public). The data is returned in a JSON file.

Here is the refactored version:

// Refactored
class Report
{
    public function getTitle()
    {
        return 'Report Title';
    }
    public function getDate()
    {
        return '2016-04-21';
    }
    public function getContents()
    {
        return [
            'title' => $this->getTitle(),
            'date' => $this->getDate(),
        ];
    }
}
interface ReportFormattable
{
    public function format(Report $report);
}
class JsonReportFormatter implements ReportFormattable
{
    public function format(Report $report)
    {
        return json_encode($report->getContents());
    }
}

In this version the results are different. Rather than just producing a JSON file the interface..

which allows you to create code which specifies which methods a class must implement, without having to define how these methods are implemented

A method is a function within a class

Clone this wiki locally