Please wait

Creating Objects and Classes

The first step in object-oriented programming is to define a class. In simple terms, a class in programming is like a blueprint or recipe. It outlines a set of instructions to create something. That 'something' is what we call an object.

For example, let's compare a class to a recipe for a chocolate cake. The recipe itself isn't a cake, but it tells you how to make a chocolate cake. It includes the ingredients (like flour, sugar, cocoa) and the steps (like mixing, baking). In this analogy, the recipe is the 'class', and the cake you bake following the recipe is the 'object'.

Defining a class

We define a class using the keyword class followed by the name of the class. Afterward, you must add a pair of curly brackets ({}) to indicate the body of the class. This is where you would define properties and methods related to the class. Here's a basic example:

class Car {
  // Here we'll put properties and methods
}

This is a class named Car. Right now, it's empty, but we could add 'properties' (like color, make, model - these are like the ingredients in our cake analogy) and 'methods' (like drive, stop, honk - these are like the baking steps in our cake analogy) to it.

Once we have our class (or recipe), we can use it to create many individual 'objects' (or cakes). So you could create a red car, a blue car, a green car - all from the 'Car' class.

Naming convention

In PHP, a common naming convention for classes is to use pascal case, which means the first letter of each word is capitalized, and there are no underscores between words. For example:

class Car {
  // Class definition
}
 
class ElectricCar {
  // Class definition
}

In this example, Car and ElectricCar follow the pascalcase convention. The name clearly represents what the class is or does, and there are no underscores or spaces between words.

Creating an object

Creating an object from a class is a bit like making a cake from a recipe. The class is the recipe, and the object is the cake that you end up with. We call this process "instantiating" a class, which in simpler terms means "creating an object from a class".

You create an object from a class using the keyword new followed by the name of the class. Here's an example:

$myCar = new Car();

In this example, $myCar is now an object of the class Car. You've created (or "instantiated") a new Car object, and you're storing it in the variable $myCar.

Why would we store an object in a variable? Well, when you bake a cake, you don't just leave it in the oven. You take it out and put it on a plate so you can use it. In the same way, we store the object in a variable so that we can use it later in our code. We can call methods on it or access or modify its properties.

Multiple instances

A specific copy of an object is referred to as an instance. This implies that multiple instances can be created, which is the correct assumption! Let's say we have a class named Dog. This class would represent a general idea of what a dog is, but we want to create specific dogs, like Rex and Bella. We can do this by creating an object for each dog:

class Dog {
  // This is our class (our dog blueprint)
}
 
$dog1 = new Dog(); // Rex
$dog2 = new Dog(); // Bella

Here, $dog1 and $dog2 are separate instances (or objects) of the Dog class. Each instance represents a unique dog. We could add properties to our Dog class (like name, breed, age), and each of these dogs could have different values for those properties.

Creating multiple objects from the same class is like baking several cakes from the same recipe, but maybe with different flavors or toppings. This is useful because it allows you to create individual objects that have the same basic structure (defined by the class) but with their own unique data or behavior.

For example, Rex and Bella can both bark, but maybe they bark in different ways, or maybe they are different breeds. Having separate instances allows us to represent these differences while still following the same general blueprint of what a dog is.

Properties

Properties in classes are kind of like characteristics or features that describe an object. Think about a real-world dog. What describes a dog? Well, it has a name, a breed, an age, and a color. In the world of PHP and classes, these would be the properties of the Dog class.

Here's how you might define a Dog class with these properties:

class Dog {
  public $name;
  public $breed;
  public $age;
  public $color;
}

In this example, $name, $breed, $age, and $color are properties of the Dog class. Along with the property name, we must add the public. The public keyword means these properties can be accessed from outside the class. Properties can be restricted, which is something we'll talk more about in a future lesson. For now, using the public keyword will suffice.

To read a property, you would first create an object from the class and then use the -> symbol. Let's create a dog named 'Rex' and read his name:

$rex = new Dog();
$rex->name = 'Rex';
 
echo $rex->name; // This will output 'Rex'

As you can see, the property does not need to include the $ symbol. PHP automatically assumes you're trying to access a property, so you can omit it after the -> operator.

To update a property, you use the -> operator again. Let's say 'Rex' just had a birthday and is now 5 years old:

$rex->age = 5;

Now the $age property for the 'Rex' object is 5. If you wanted to check, you could echo it out just like we did with the name:

echo $rex->age; // This will output '5'

So, to sum up, properties are the things that describe an object, and you can read or update these properties by using the -> symbol (also known as the object operator).

Methods

Methods in classes are like actions or behaviors that an object can perform. If you think about a real-world dog, what actions can a dog do? It can bark, eat, sleep, or play. In the world of PHP and classes, these would be the methods of the Dog class.

Here's how you might define a Dog class with a bark method:

class Dog {
  public $name;
 
  public function bark() {
    echo $this->name . " says woof!";
  }
}

In this example, bark is a method of the Dog class. It's defined just like a regular PHP function but inside the class. The public keyword means this method can be called from outside the class. The $this->name inside the bark method refers to the $name property of the current object.

What is the $this variale?

The $this variable in PHP is a special variable that's used within a class to refer to the current object. In other words, it represents the instance of the class in which it is used.

Think of $this as a way for an object to refer to itself. For example, when you're talking about yourself, you might say, "I am going to the store." In PHP classes, $this is the object's way of saying "I". This way, you don't accidentally access properties or methods from another class or the wrong instance.

To call a method, you would first create an object from the class and then use the -> symbol, followed by the method name and parentheses. Let's create a dog named 'Rex' and make him bark:

$rex = new Dog();
$rex->name = 'Rex';
 
$rex->bark(); // This will output 'Rex says woof!'

Just like we did with properties, we use the -> symbol to call methods. The difference is that we also add parentheses after the method name.

To read a property, you also use the -> symbol, but without parentheses. So, methods are the actions that an object can perform, and you can call these actions (or methods) by using the -> operator followed by the method name and parentheses.

Key takeaways

  • A class in PHP is like a blueprint or a template for creating objects.
  • You define a class using the class keyword, followed by the name of the class.
  • The name of a class usually follows the PascalCase naming convention, with the first letter of each word capitalized and no underscores.
  • Inside a class, you can define 'properties' and 'methods'. Properties are characteristics of the class (like a dog's name or color), and methods are actions the class can perform (like a dog barking).
  • You create an object from a class (or "instantiate" a class) using the new keyword. Each object is a separate instance of the class with its own set of property values.
  • To access or modify an object's properties or to call an object's methods, you use the -> operator (object operator).
  • Inside a class, the $this variable is used to refer to the current instance of the class. It allows an object to access its own properties and methods.

Comments

Please read this before commenting