Classes and Objects

Updated onbyAlan Morel
Classes and Objects

Classes and Objects are fundamental to many programming languages, and PHP is no different. In this lesson we'll learn about what the two are and how you can use them to improve your PHP code.

A Class in PHP is a blueprint that you define that can then be used to create individual Objects. A class typically has variables associated to them, called properties, and functions that are internally defined that belong to that class.

Defining a Class

Defining a class in PHP is done with the built-in class keyword. Let's say you run a farm and you want to keep track of all the fruits you harvested. Let's create a class that represents a single fruit:

PHP
<?php class Fruit { } ?>

That's literally it. We have now defined a new class called Fruit to use to keep track of every fruit. Now, let's make this class useful!

Constructing a Class

In most cases, to create a new instance of the class, or object, you will need to call the constructor for the class. This function can optionally take any parameters needed to create a new instance and will create the instance and return it to you. Let's give our Fruit class a constructor and create a new fruit with it.

PHP
<?php class Fruit { // Declare class properties public $weight = 0; // Declare constructor to create new Fruit public function __construct($weight) { $this->weight = $weight; } // Public function to get the weight public function getWeight() { return 'This fruit weighs ' . $this->weight . ' grams.'; } } $fruit = new Fruit(250); var_dump($fruit); echo($fruit->getWeight()); ?>
HTML
object(Fruit)#1 (1) { ["weight"]=> int(250) } This fruit weighs 250 grams.

Okay let's break down what's going on here.

At the top of the class we are defining a single property for the class, the weight of the fruit. Then we are defining the constructor that will automatically get called when we run new Fruit(). The constructor will take the weight we passed in and set it to the new fruit's weight. Finally, we create a function that returns a string containing the fruit's weight.

To show what the values are, we then use var_dump() on the entire variable, then use echo() on the return value of the getWeight() function.

Now let's illustrate just how useful classes are by creating three fruits:

PHP
<?php class Fruit { public $weight = 0; public function __construct($weight) { $this->weight = $weight; } public function getWeight() { return 'This fruit weighs ' . $this->weight . ' grams.'; } } $fruit1 = new Fruit(250); echo($fruit1->getWeight()); $fruit2 = new Fruit(300); echo($fruit2->getWeight()); $fruit3 = new Fruit(150); echo($fruit3->getWeight()); ?>
HTML
This fruit weighs 250 grams. This fruit weighs 300 grams. This fruit weighs 150 grams.

It just takes a single additional line of code to create an entirely new Fruit object, each with their own independent weights. Also, feel free to easily modify an object's property values either directly or through a function:

PHP
<?php class Fruit { public $weight = 0; public function __construct($weight) { $this->weight = $weight; } public function getWeight() { return 'This fruit weighs ' . $this->weight . ' grams.'; } public function setWeight($newWeight) { $this->weight = $newWeight; } } $fruit = new Fruit(250); echo($fruit->getWeight()); $fruit->weight = 300; // changing value directly echo($fruit->getWeight()); $fruit->setWeight(400); // changing value through a function echo($fruit->getWeight()); ?>
HTML
This fruit weighs 250 grams. This fruit weighs 300 grams. This fruit weighs 400 grams.

Class Inheritance

Class inheritance is used to create classes that inherit the properties and functions defined in another class. Using class inheritance, you can define a new "child" class that is more specific than its "parent" class. In our scenario, let's make a class to represent an apple. An apple is also a fruit, so it makes sense to inherit from the Fruit class we defined earlier.

PHP
<?php class Apple extends Fruit { public function getType() { return 'I am an apple!'; } } $apple = new Apple(300); echo($apple->getType()); echo($apple->getWeight()); ?>
HTML
I am an apple! This fruit weighs 300 grams.

How cool is that? Even though we created a new apple, because we extended the Fruit class, our apple is also a fruit, just like they are in real life. That means that it gets access to both the properties and functions defined in the Apple class, but also the properties and functions defined in the Fruit class, which is why we were still able to do:

PHP
echo($apple->getWeight());

Overriding

Let's expand on the functionality of the Apple class by defining a property only accessible to it. Since apples can be red, green, or yellow, let's allow ourselves to give our apples a color:

PHP
<?php class Fruit { public $weight = 0; public function __construct($weight) { $this->weight = $weight; } public function getWeight() { return 'This fruit weighs ' . $this->weight . ' grams.'; } } class Apple extends Fruit { public $color = ""; function __construct($weight, $color) { // this will call the parent constructor parent::__construct($weight); $this->color = $color; } public function getColor() { return 'This apple is ' . $this->color . '.'; } public function getWeight() { return 'This apple weighs ' . $this->weight . ' grams.'; } } $apple = new Apple(300, 'red'); echo($apple->getWeight()); echo($apple->getColor()); ?>
HTML
This apple weighs 300 grams. This apple is red.

Quite a few interesting things are happening in the above code. First off, if you noticed in the constructor for Apple, we are manually calling the constructor for Fruit to pass in the apple's weight. This is what, from PHP's perspective, allows the apple to also be a fruit, since we are fulfilling all the requirements for it to be a fruit. The second parameter, red, gives the apple its color by setting its $color property.

Also, in the Apple class, we are defining a function named getWeight(), which is the exact same name of the function in the Fruit class. This is called overriding a function, because PHP will use the child's implementation of the function, instead of the parent one. That is why the string read This apple weighs 300 grams. instead of This fruit weighs 300 grams..

Classes and Objects in PHP are very versatile and useful at representing data in a compact and easy-to-work-with package. You just define a class, maybe define another class that inherits from that one, and you can create endless objects from them!

Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.