Skip to Content
Learn
Classes and Objects
What are Classes?

In the previous exercise, we asked you to imagine a class for pets. Let’s see how we would actually create a PHP Pet class.

To define a Pet class, we use the class keyword followed by the class name (typically title cased in PHP) and curly brackets:

class Pet { }

Within the curly brackets, we can add properties, which define the data each object of the class will contain. The syntax is similar to how we define variables:

class Pet { public $name, $color; }

Note: The public keyword has to do with something called visibility. We’ll discuss this in depth later in the lesson.

Instructions

1.

Let’s practice defining classes. Start by defining a Beverage class.

2.

Now add some properties to Beverage:

  • color
  • opacity
  • temperature
Folder Icon

Take this course for free

Already have an account?