Classes and objects in PHP

Object-oriented programming is useful to create useful abstractions and make our code simpler to understand and manage.

To start with, you have classes and objects.

A class is a blueprint, or type, of object.

For example you have the class Dog, defined in this way:

class Dog {

}

(it must be defined uppercase)

Then you can create objects from this class. Specific, individual dogs.

An object is assigned to a variable, and it’s instantiated using the new Classname() syntax:

$roger = new Dog();

You can create multiple objects from the same class, by assigning each object to a different variable:

$roger = new Dog();
$syd = new Dog();

We’ll learn more OOP concepts in the next few tutorials

Download my free PHP Handbook!