PHP Objects and Classes

Summary: in this tutorial, you will learn about basic object-oriented programming(OOP). We will introduce you the most important concepts in PHP OOP: objects and classes.

If you follow our tutorial from the beginning, you will see that we’ve organized our tasks into the functions that manipulate data in the application. And we execute a PHP page by calling one function to the next.  This technique is known as procedural programming.

In object-oriented programming, we take a whole different approach. We model our problems and processes using objects. Therefore, an object-oriented application consists of related objects that collaborate with each other to solve the problem.

The basic building blocks of object-oriented programming are classes and objects. Understanding object and class and the differences between object and class is very important before we can dive into object-oriented programming approach.

PHP objects vs. classes

In the real world, an object has its own characteristics and behaviors. We can group similar objects with the same characteristics and behaviors in into one class. So we can say that a class is a blueprint of the objects.

For example, a class is a blueprint for a house. A blueprint defines characteristics of the house on paper. From the blueprint, we can build as many houses as we want. We say an object is an instance of the class, or a house is an instance of the blueprint.

PHP OOP Objects vs. Classes

PHP OOP Objects vs. Classes

Defining classes and objects

Before creating any new object, you need a class or blueprint of the object. It’s pretty easy to define a new class in PHP. To define a new class in PHP you use the class keyword as the following example:

We’ve defined a new empty class named BankAccount. From the BankAccount class, we can create new bank account object using the new keyword as follows:

Notice that we use var_dump() function to see the content of the bank account.

Properties

In object-oriented terminology, characteristics of an object are called properties. For example, the bank account has account number and total balance. So let’s add those properties to the BankAccount class:

You see that we used the private keyword in front of the properties. This is called property visibility. Each property can have one of three visibility levels, known as private, protected and public.

  • private: private properties of a class only can be accessible by the methods inside the class. The methods of the class will be introduced a little later.
  • protected: protected properties are similar to the private properties except that protected properties can be accessible by the subclasses.  You will learn about the subclasses and inheritance later.
  • public: public properties can be accessible not only by the methods inside but also by the code outside of the class.

In the BankAccount class, we can only access those properties of the bank account inside the class.

Methods

Behaviors of the object or class are called methods. Similar to the class properties, the methods can have three different visibility levels: private, protected, and public.

With a bank account, we can deposit money, withdraw money and get the balance. In addition, we need methods to set the bank account number and get the bank account number. The bank account number is used to distinguish from one bank account to the other.

To create a method for a class you use the function keyword followed by the name and parentheses. A method is similar to a function except that a method is associated with a class and has a visibility level. Like a function, a method can have one or more parameter and can return a value.

Let’s add some methods to the BankAccount class:

Notice that we used a special $this variable to access a property of an object. PHP uses the $this variable to refer to the current object.

To access a property you use the arrow (->) operator as follows:

Let’s examine the BankAccount's methods in greater detail:

  • The deposit() method increases the total balance of the account.
  • The withdraw() method checks the available balance. If the total balance is less than the amount to be withdrew,  it issues an error message, otherwise it decreases the total balance.
  • The getBalance() method returns the total balance of the account.
  • The setAccountNumber() and getAccountNumber() methods are called setter/getter methods.

Calling methods

To call a method of an object, you use the (->) operator followed by the method name and parentheses. Let’s call the methods of the bank account class:

In this tutorial, you have learned the basic PHP object-oriented programming (OOP). You also learned the two important building blocks of PHP OOP, objects, and classes.

  • Was this tutorial helpful ?
  • YesNo