PHP objects and classes for beginners

This is a beginner tutorial on PHP objects and classes.

What is an object?

In the real world, an object is something that can be seen or touched. It is a thing. For example, it could be a car, a dog, a light bulb, a person, or a flower pot.

Take a moment to look at the objects that surround you. Look at your monitor, the cup of coffee on your desk, or the smart phone that is resting in your hand.

These are all objects.

In PHP, objects are “things” that have attributes and behaviors, just like any real-life object.

Let’s look at an example of a car, which has the following attributes:

  • Color.
  • Length and width.
  • Petrol or diesel.
  • Engine size.
  • Number of doors.
  • Automatic or manual.

These are all attributes. In some cases, these attributes will have an effect on the behavior of the car. For example, a car’s engine size will have an impact on its speed.

Now, let’s list a car’s behaviors:

  • It can start.
  • Drive.
  • Stop.
  • Change gears.

In PHP, attributes are called properties, and behaviors are referred to as class functions or methods.

To sum it up: Property = attribute. Function/method = behavior.

What is a PHP class?

A class is a blueprint for an object. It defines what an object is (properties), and it defines what an object can do (functions).

Think of it as an architectural drawing.

Creating a class in PHP.

If you want to create an object in PHP, you will need to learn how to create a class.

class Car{

    public $color = 'Red';

    public function drive(){
       //Code that drives the car is placed here.
    }

}

If you look at the PHP code above, you’ll see that we created a class called “Car”. There are two things that you should notice here:

  1. There is a property called $color.
  2. There is a class function called drive.

In this case, drive is a behavior and $color is an attribute. As you can see, we’ve set the $color property to red.

Creating a PHP object.

Now, what if we want to create an object from our class? Well, we will need to instantiate it.

//Make sure that our class file has been included.
include 'classes/Car.php';

//Create our car object by creating an "instance" out of the class.
$car = new Car();

We have now created a car object and assigned it to the variable $car.

If our class was called “Person” instead of “Car”, then we would have had to instantiate it like this:

$person = new Person();

Remember: Class names are case-sensitive.

We have created our car object. Now, let’s drive it using the drive function in our class:

//Create the object.
$car = new Car();

//Call the class function "drive", which we created in our class file.
$car->drive();

When we created our object, it acquired the functions that we created in our class file.

Essentially, those functions are actions that our object can carry out. In our case, we created an example function called drive.

What if we want to add another “behavior” to our object?

class Car{

    public $color = 'Red';

    public function drive(){
       //Code that drives the car is placed here.
    }

    public function stop(){
       //Code that stops the car is placed here.
    }

}

As you can see, we’ve simply added another function called “stop” to our Car class. This means that our car object can now “drive” and “stop”.

Objects in the world of PHP.

In PHP, we don’t create cars or coffee mugs. Instead, we create objects that represent users, database tables, blog posts, or shopping carts.

For example, a “user” in a PHP application may have the following properties (attributes):

  • Username.
  • Email address.

A “user” might also have the following functions (behaviors):

  • Login.
  • Logout.
  • Post a status on their profile.

What would a user class look like in PHP?

class User{

    public $username;

    public $emailAddress;

    public function login($username, $password){
        //Login code goes here.
    }

    public function logout(){
        //Logout code goes here.
    }

    public function postStatus($status){
        //Insert status into database, etc.
    }

}

In the code above, we created a basic PHP class called User.

It has two properties: $username and $emailAddress. It has three functions (behaviors) called login, logout, and postStatus, which we can call after we instantiate the class and create a user object.

Now, let’s create a function called setEmail, which will allow us to modify our $emailAddress property:

class User{

    public $username;

    public $emailAddress;

    public function login($username, $password){
        //Login code goes here.
    }

    public function logout(){
        //Logout code goes here.
    }

    public function postStatus($status){
        //Insert status into database, etc.
    }

    public function setEmail($emailAddress){
        $this->emailAddress = $emailAddress;
    }

}

We can now use our new function:

include 'User.php';
$user = new User();
$user->setEmail('[email protected]');
$user->logout();

This concludes our introduction to PHP objects and classes.

If you still don’t fully understand object-oriented programming, don’t worry, as it can take a while for it to click. This is one of those topics that you have to ease yourself into. In the meantime, you should create your own test classes based on the examples above. Play around with different object types, functions, and properties.