Object Oriented Programming in PHP

12th April, 2016

I had a question from one of the people I'm mentoring on http://phpmentoring.org.

How do I decide what is an object and what should be a method?

This may seem a trivial question to someone who as been writing object orientated (OO) code for a while, but I can remember learning about objects and being perplexed by their properties, inheritance and much more when I started learning.

This is intended to be an introductory level article, some basic knowledge of PHP is assumed but not much OO experience.

Firstly, what is an object?

Put simply, it is something in your application that can have both state (properties) and also behavior (methods) that usually represents something in real life (we call this modeling our domain).

We can design objects wherever it makes sense to: for introducing a new piece of logic, or creating a feature or part of it. A well designed object should do (or represent) one thing - this is called SRP or the Single Responsibility Principle. There is no limit to the number of objects we can create or use in an applications, it is often better to have lots of small, simple objects that can be composed together to make up a larger application.

Smaller, simpler objects are:

  • easier to understand, read and reason about
  • easier to re-use (in this application and other applications)
  • easier to change or replace in the future
  • easier to unit test
  • easier to name if it has only one responsibility or capability
  • will probably have less dependencies on other code
  • easier to debug when things go wrong

Properties and Methods

Properties are like variables that belong to the object. They can be set to any primitive types (strings, integers, floats, arrays) or even other objects, and they can change or mutate over time.

Methods are similar to functions except they belong, or relate, to the object. They are normally split into imperative (commanding) or interrogative (questioning) categories, and ideally each method will do one thing only.

Properties and methods can have different visibilities (public, private or protected) which alter the scope of the property or method, and affect how other objects can interact with it.

  • public means fully accessible to any other objects or calls outside of the object itself.
  • private means only accessible within the object (i.e. from internal methods). No outsider can access the private properties or methods.
  • protected is similar to private, except if another class extends an object with protected properties/methods then these can be accessed from the child as well as the parent.

What is a class?

A class is often called the blueprint for an object. An object is an instance of a class. To create an instance of a class (object) you typically use the new keyword, such as new MyClass();. Using the new keyword calls a special reserved method on the class called __construct(). The constructor may take parameters or set up internal properties of the object if needs be. Any parameters or dependencies the object needs to be considered valid should be passed at construction time, otherwise you may end up with invalid or incomplete objects.

Defining a class

<?php

class Student
{
    private $name;
    private $dob;

    public function __construct(string $name, DateTime $dob)
    {
        $this->name = $name;
        $this->dob = $dob;
    }

    public function age(): Int
    {
        return $this->dob->diff(new DateTime())->y;
    }
}

Using the above class

<?php

$pete = new Student('Pete', new DateTime('1 June 1988'));

echo $pete->age();

Category: oop

Tagged: oop php design mentoring