Web Development With HHVM and Hack 12: Classes Part 1

Mike Abelar
5 min readApr 7, 2020

--

In the last tutorial, we covered Enums in Hack: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-11-enums-2653782bf5d9. In this tutorial, we will begin covering classes.

What Are Classes?

Classes are blueprints to represent properties and the interactions of those properties. We will consider the simple example that is on the official Hack documentation: a 2D point. When making a class, the first two questions we need to answer are: 1. what properties does the class have? and 2. what interactions and operations does the class have?

Let us first tackle the first: what properties does the class have? To answer this, what properties does a 2D point have? Well, it has an x coordinate and a y coordinate. Once we identify the properties, we need to identify the types of these properties. Because coordinates can be any number (positive or negative and whole or decimal), we will represent coordinates with a float. We will cover types in a future tutorial. For now, you can find a list of types here. The final question we will need to answer about our properties is their visibility. Visibility of an property refers to the ability of other classes to access and view the property. Most of the time the visibility of properties will be private, meaning only the class itself can access and modify the property. This is the best practice as properties should be accessed and modified via functions.

Let us explore classes and see what we have so far. To explore classes, create a new folder for this tutorial. Inside the folder create a touch .hhconfig file and a file to explore classes: touch classes.hack . Inside the Hack file post the following contents:

class Point {
private float $x_coordinate;
private float $y_coordinate;
}
<<__EntryPoint>>
function main() : noreturn {
exit(0);
}

We do not need to run this code because it does nothing. However, let’s take note of the Point class and see how class properties are made. We create a class with the class keyword followed by the name of the class (Name should be upper case). Then, we see the two properties we were discussing above. We declared them to be private and have type float. Notice, that we define the name and end the line with a semicolon. We do not assign any value. Therefore, one might ask, what is the value of the properties? We need to define functions to set them and access them.

The Constructor

When we make a class, we have an opportunity to set the initial values of our properties. We do this in the constructor. The constructor automatically gets called each time we make a new instance of our class. Let us see an example in the code:

class Point {
private float $x_coordinate;
private float $y_coordinate;
public function __construct(float $x, float $y) {
$this->x_coordinate = $x;
$this->y_coordinate = $y;
}
}
<<__EntryPoint>>
function main() : noreturn {
exit(0);
}

Like before, no code can be run. Let us take a look at the constructor. The constructor will always begin with: public function __construct( as the constructor should always be public. Public means that any class or any code can access the function. We take in two float variables, x, and y which will then be turned into the values for the properties. The $this-> syntax is an operator to get the class property. this refers to the instance of the class. An instance of a class is one example of the class that was made. A class can have multiple instances, each separate from each other.

Let us verify that our constructor worked by adding class functions to get the value of our properties:

class Point {
private float $x_coordinate;
private float $y_coordinate;
public function __construct(float $x, float $y) {
$this->x_coordinate = $x;
$this->y_coordinate = $y;
}
// gets the the value of the x coordinate
public function get_x_coordinate() : float {
return $this->x_coordinate;
}
// gets the the value of the y coordinate
public function get_y_coordinate() : float {
return $this->y_coordinate;
}
}
<<__EntryPoint>>
function main() : noreturn {
// create a new instance of the class
// when calling new Point(), we invoke the constructor
// so inside (), we need to pass the value for x and y
$point = new Point(3.3, 4.4);
// this is the syntax for calling the class function
print($point->get_x_coordinate());
print("\n");
print($point->get_y_coordinate());
print("\n");
exit(0);
}

Indeed, if we run the code (by typing hhvm classes.hack), we get 3.3 and 4.4. Let us see what we did. We first created two functions that get the current values of the properties so we can later print them in main() . Then, in main, we make a new instance of the class using the new keyword. We store the instance of the class in a variable called point . point can then call its class functions to get the current property values and display them.

Setting Values

Getting the current values are great. Let’s add support for setting the value of x and y:

class Point {
private float $x_coordinate;
private float $y_coordinate;
public function __construct(float $x, float $y) {
$this->x_coordinate = $x;
$this->y_coordinate = $y;
}
// gets the the value of the x coordinate
public function get_x_coordinate() : float {
return $this->x_coordinate;
}
// gets the the value of the y coordinate
public function get_y_coordinate() : float {
return $this->y_coordinate;
}
// sets the value of the x coordinate
public function set_x_coordinate(float $new_x) : void {
$this->x_coordinate = $new_x;
}
// sets the value of the y coordinate
public function set_y_coordinate(float $new_y) : void {
$this->y_coordinate = $new_y;
}
}
<<__EntryPoint>>
function main() : noreturn {
// create a new instance of the class
// when calling new Point(), we invoke the constructor
// so inside (), we need to pass the value for x and y
$point = new Point(3.3, 4.4);
// change the initial values
$point->set_x_coordinate(1.1);
$point->set_y_coordinate(2.2);
// this is the syntax for calling the class function
print($point->get_x_coordinate());
print("\n");
print($point->get_y_coordinate());
print("\n");
exit(0);
}

This code creates two functions which can set the x and y coordinates. The output of this program is 1.1 and 2.2 which works as expected.

In the next tutorial, we will cover interfaces: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-13-classes-part-2-4a6d2d96597f

--

--