Object-Oriented PHP

Object-Oriented Programming, or OOP for short, is based around the concept of Objects, where an Object is made up of Attributes, or Variables, that can contain data, and Methods, that describe actions of an Object. A person can be used as a real-world example of an Object. A person has Attributes such as height, weight, eye colour, hair colour and so on, as well as Methods or actions such as, walk, talk and eat.

A Class is a blue print, or template, of an Object that describes its Attributes and Methods. From this template one or more Objects can be created, which is known as instantiating an Object. A Class can be compared to an architects drawing of a building, which describes how it needs to be built. From this drawing, one or more buildings can be made to the specification of the drawing.

In PHP, the basic structure of a Class is as follows.

class ClassName
{
}

Notice the capitalisation of the first letter of each word in the Class name. This is known as Pascal Case and is the standard method for naming Classes in PHP. Usually, when a Class is created it is placed in its own PHP file, and then the 'include' or 'require' statement is used to include the Class where needed. By convention Classes are located in a 'classes' folder.

Below is an example of a class Called 'Greeting', along with how it can be instantiated, using the name '$greeting'.

class Greeting
{
  
}

$greeting = new Greeting();

Adding Properties

Properties are added to Classes in PHP in the form of Variables. The difference between Variables in Classes and those discussed earlier is that the declaration is preceded by an Access Modifier. An Access Modifier controls the visibility of a Property and can either be 'public', 'private' or 'protected'. These are explained below.

  • Public - A Property is directly accessible from within a Class, as well as outside of the Class.
  • Private - A Property is only directly accessible from within the Class that it is defined.
  • Protected - A Property is directly accessible from within the Class that it is defined, as well as within any child Classes.
class Greeting
{
  public $message = "Hello World!";
}

$greeting = new Greeting();

echo $greeting->message;

Here a property, '$message', has been added to the 'Greeting' class with the 'public' Access Modifier. After the Object has been instantiated, the message is echoed out to the screen. Notice that the '$' sign is used when referencing the Object, in this case 'greeting', but not with the 'messsage' Property.

Setting Properties

Setting a Property of an Object is done in a similar way to accessing it. In this example a 'name' Property has been added to the 'Greeting' class. This is set to 'Fred' and concatenated with the rest of the message before being output to the screen.

class Greeting
{
  public $message = "Hello ";
  public $name = "";
}

$greeting = new Greeting();

$greeting->name = "Fred";
echo $greeting->message . " " . $greeting->name . "!";

Adding Methods

Functions in a Class in PHP are known as Methods and carry out the actions of an Object. In the previous example the message is formed by concatenating two Properties together, separated by a space, with an exclamation mark on the end. This formatting of the message could be carried out in a Method. The advantage of doing this is that it could be reused any number of times and if the format of the message needed changing it only needs to be altered in one place. In the 'return' statement, '$this' is used to reference the current object. The difference between referencing a Property and a Method is that with a Method the name is followed by '()', as with 'displayGreeting()' below. Access Modifiers are used in the same way with Methods as they are with Properties.

class Greeting
{
  public $message = "Hello ";
  public $name = "";
  
  public function displayGreeting()
  {
    return $this->message . " " . $this->name . "!";
  }
}

$greeting = new Greeting();

$greeting->name = "Fred";
echo $greeting->displayGreeting();

Getters and Setters

With Properties it is good practice to provide Getter and Setter Methods instead of allowing direct access from outside of the Class. This allows for things such as validation before a Property is set, or checking if a user is authorised to access a Property, for example. Even if there is no initial need for any kind of processing before getting or setting a Property, for future proofing purposes it is still a good idea to create Getters and Setters for each Property where needed. With this in mind the above example can be re-written as follows. Both the Properties now have the 'private' Access Modifier because they are now only accessed from within the Class. In order for the 'name' Property to be set, a 'setName' Method has been created. The 'getName' Method has been added as an example only, as this isn't currently needed. As the 'message' Property is only accessed from within the Class it doesn't need a Getter or Setter.

class Greeting
{
  private $message = "Hello ";
  private $name = "";
  
  public function getName()
  {
    return $this->name;
  }
  
  public function setName($name)
  {
    $this->name = $name;
  }
  
  public function displayGreeting()
  {
    return $this->message . " " . $this->name . "!";
  }
}

$greeting = new Greeting();

$greeting->setName("Fred");
echo $greeting->displayGreeting();