A class can be thought of as a set of blueprints that describe something. From this set of blueprints, individual examples can be made. These examples, often called instances, are single objects. If we take pets as an example, we can use a class to describe pets in general. The class will tell us that the pet should have a name, an age, and a species, among other pieces of information. It is only when making an instance of a class that we get more specific details about these pieces of information. By creating an instance, we go from knowing to expect a pet to have a name, age, and species to knowing that we are talking about a 7 year old dog, named Baby.

To create a class, start with the following format. Although the name of a class does not need to be capitalized, it is conventional to do so.

class Name{
//class information
}

A class is assigned characteristics using attributes. An attribute can be any piece of data that describes the object that the class represents. For example, a pet class could be written as follows.

class Pet{
var $name;
var $age;
var $species;
}

For the pet class, name, age, and species are attributes of the class. The “var” keyword before the attributes indicates that these properties are attributes of the class. There are four different keywords that are used to accomplish this, “var”, “public”, “private”, and “protected”. These four keywords are called visibility modifiers. They let the PHP program know how these attributes can be accessed.

Public attributes can be accessed and modified by anyone. The “var” keyword works the same way as the “public” keyword; they are interchangeable. Although they work in the same way, “public” is more commonly used than “var”. The “private” keyword allows attributes to be accessed only within the class where it is defined. The “protected” keyword allows attributes to be accessed only within a class.

A new pet would be created by using the following code.

$pet1 = new Pet;

This would create a new pet object which would be identified as $pet1. This pet object is an instance of the Pet class. Attributes for this pet object are defined using the following format.

$pet1->name = "Baby";
$pet1->age = 7;
$pet1->species = "Dog";

Now, the $pet1 object is a dog named Baby who is 7 years old.

This format can also be used to reassign the attributes of an object after it is created.

Constructors

A constructor is a function that is called each time an object is created from a class. A constructor is created inside a class and is written in the following format.

function __construct(parameters){ }

The code that the function will execute goes inside the curly braces. Inside the parentheses, parameters can be added, as with other functions. The constructor function can make it much simpler to create a new instance of a class. The attributes of an object can be passed into the constructor function as parameters. The values that a user passes into the constructor can be assigned to the class attributes.

class Pet{
var $name;
var $age;
var $species;
function __construct( $petName, $petAge, $petSpecies){
$this-> $name = $petName;
$this-> $age = $petAge;
$this-> $species = $petSpecies;
}
}

Object Functions

An object function is a function that is defined within a class. The instances created with this class can use this function and these functions can be called on class instances.

Two common types of object functions are getters and setters. Getters are used to retrieve pieces of information about a class and its attributes. Setters are used to alter or manipulate pieces of information about a class and its attributes.

For example, let’s say we’ve had our pet from the previous example for a whole year and we want to celebrate. We could use the function below to wish our pet a happy birthday. This function also updates the pet’s age to show that they are now a year older.

function happyBirthday(){
$this->age++;
echo “Happy Birthday, $this->name!<br>”;
echo “You are now $this->age years old!<br>”;
}

Conclusion

Classes are an excellent way to make modular repeatable code. The use of classes and objects in PHP allows developers to create a blueprint for objects, complete with functions that allow these objects to be used and altered, and then make instances from this blueprint.

Full Stack Software Engineer/ Web Developer and former Mechanical Engineer https://www.linkedin.com/in/ariel-jakubowski/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store