Intermediate PHP 3 – Classes/Objects

All modern programming languages have the concept of classes/objects. A class is a collection of methods (functions) and properties (variables that belong to the class).

Classes and objects are not the same thing, but they are similar. Classes are used to create objects. Classes are like the blueprint, and objects are the house. You use the blueprint to create the house. Or if you want to think of it in terms of biology, the class is like the DNA, and the object is like a cell that is created by the DNA.

class Cell
{
    private $nucleus;
    public $cellWall;

    public function __construct ($nucleus, $cellWall)
    {
        $this->nucleus = $nucleus;
        $this->cellWall = $cellWall;
    }

    public function secreteExtracellularMatrixPrecursors()
    {
        // super complex code to secrete extracellular matrix precursors
    }
}
$fibroblastCell = new Cell('fibroblast necleus', 'fibroblast cell wall');
$fibroblastCell->secreteExtracellularMatrixPrecursors();
echo $fibroblastCell->nucleus;

Cells in the human body have properties and functions, just like classes.

Code Dissection

class Cell

Like the special word “function”, the special word “class” tells PHP we’re defining a class. The names of classes are always capitalized. Every word in the name is capitalized, even the first.

private $nucleus;

The “private” word means that this property cannot be directly accessed. It’s only visible and accessible to the class itself. Visibility is a big key word and very important feature in OOP (object oriented programming). In this case, the nucleus is not visible or accessible to the outside scope, just like a real cell’s nucleus which is located inside the cell.

public $cellWall;

The cell wall of our cells will be accessible to the outside scope. It’s accessible and even modifiable after the cell has been instantiated into the world.

public function __construct($nucleus, $cellWall)

The construct function is a special, unique function found in classes . It receives data (via parameters) being passed to the class when the class is being instantiated. Any setup that needs to be done happens in the construct function.

$this->nucleus = $nucleus;

Usually, the main thing we do in the construct is take the data passed in as parameters and set them to the class’s properties. The rest of the class can then access these properties.

public function secreteExtracellularMatrixPrecursors()

The public word is used to control the visibility of functions in a class, just like it’s used to control the visibility of the properties.

$fibroblastCell = new Cell(‘fibroblast necleus’, ‘fibroblast cell wall’);

Use the keyword new before the class name to instantiate/build/create the class. We’re passing two parameters into the class, and these get passed to the class’s construct function. These two parameters correspond to the $nucleus and $cellWall parameters of the construct function.

fibroblastCellThe instantiated class is called an object, and we’re assigning the object to the $bob variable. The variable $fibroblastCell is not a class. It’s an object, just like a cell is not the DNA that is used to create it.

$fibroblastCell->secreteExtracellularMatrixPrecursors();

We can call a class’s method by using an arrow (->), the name of the method, and parenthesis. If the secreteExtracellularMatrixPrecursors function needed any parameters, we could’ve passed them here, but the function did not need any parameters.

echo $fibroblastCell->nucleus;

To access public properties, we also use the arrow and the name of the property. We could not do this for the $nucleus property since it was private.

Classes/objects are extremely useful in PHP, and you’ll make heavy use them throughout your career.