Summary: in this tutorial, you will learn about PHP objects, how to define a class, and how to create an object from a class.
What is an Object
If you look at the world around you, you’ll find many examples of tangible objects: lamps, phones, computers, and cars. Also, you can find intangible objects such as bank accounts and transactions.
All of these objects share the two common key characteristics:
- State
- Behavior
For example, a bank account has the state that consists of:
- Account number
- Balance
A bank account also has the following behaviors:
- Deposit
- Withdraw
PHP objects are conceptually similar to real-world objects because they consist of state and behavior.
An object holds its state in variables that are often referred to as properties. An object also exposes its behavior via functions which are known as methods.
What is a class?
In the real world, you can find many same kinds of objects. For example, a bank has many bank accounts. All of them have account numbers and balances.
These bank accounts are created from the same blueprint. In object-oriented terms, we say that an individual bank account is an instance of a Bank Account class.
By definition, a class is the blueprint of objects. For example, from the Bank Account class, you can create many bank account objects.
The following illustrates the relationship between the BankAccount
class and its objects. From the BankAccount
class you can create many BankAccount
objects. And each object has its own account number and balance.
Define a class
To define a class, you specify the class
keyword followed by a name like this:
<?php
class ClassName
{
//...
}
Code language: HTML, XML (xml)
For example, the following defines a new class called BankAccount
:
<?php
class BankAccount
{
}
Code language: HTML, XML (xml)
By convention, you should follow these rules when defining a class:
- A class name should be in the upper camel case where each word is capitalized. For example,
BankAccount
,Customer
,Transaction
, andDebitNote
. - If a class name is a noun, it should be in the singular noun.
- Define each class in a separate PHP file.
From the BankAccount
class, you can create a new bank account object by using the new
keyword like this:
<?php
class BankAccount
{
}
$account = new BankAccount();
Code language: HTML, XML (xml)
In this syntax, the $account
is a variable that references the object created by the BankAccount
class. The parentheses that follow the BankAccount
class name are optional. Therefore, you can create a new BankAccount
object like this:
$account = new BankAccount;
Code language: PHP (php)
The process of creating a new object is also called instantiation. In other words, you instantiate an object from a class. Or you create a new object from a class.
The BankAccount
class is empty because it doesn’t have any state and behavior.
Add properties to a class
To add properties to the BankAccount
class, you place variables inside it. For example:
<?php
class BankAccount
{
public $accountNumber;
public $balance;
}
Code language: HTML, XML (xml)
The BankAccount
class has two properties $accountNumber
and $balance
. In front of each property, you see the public
keyword.
The public
keyword determines the visibility of a property. In this case, you can access the property from the outside of the class.
To access a property, you use the object operator (->
) like this:
<?php
$object->property;
Code language: HTML, XML (xml)
The following example shows how to set the values of the accountNumber
and balance
properties:
<?php
class BankAccount
{
public $accountNumber;
public $balance;
}
$account = new BankAccount();
$account->accountNumber = 1;
$account->balance = 100;
Code language: HTML, XML (xml)
Besides the public
keyword, PHP also has private
and protected
keywords which you’ll learn in the access modifiers tutorial.
Add methods to a class
The following shows the syntax for defining a method in a class:
<?php
class ClassName
{
public function methodName(parameter_list)
{
// implementation
}
}
Code language: HTML, XML (xml)
Like a property, a method also has one of the three visibility modifiers: public
, private
, and protected
. If you define a method without any visibility modifier, it defaults to public
.
The following example defines the deposit()
method for the BankAccount
class:
<?php
class BankAccount
{
public $accountNumber;
public $balance;
public function deposit($amount)
{
if ($amount > 0) {
$this->balance += $amount;
}
}
}
Code language: HTML, XML (xml)
The deposit()
method accepts an argument $amount
. It checks if the $amount
is greater than zero before adding it to the balance.
To call a method, you also use the object operator (->
) as follows:
$object->method(arguments)
Code language: PHP (php)
The new syntax in the deposit()
method is the $this
variable. The $this
variable is the current object of the BankAccount
class.
For example, when you create a new object $account
and call the deposit()
method, the $this
inside the method is the $account
object:
$account = new BankAccount();
$account->accountNumber = 1;
$account->balance = 100;
$account->deposit(100);
Code language: PHP (php)
Similarly, you can add the withdraw()
method to the BankAccount
class as follows:
<?php
class BankAccount
{
public $accountNumber;
public $balance;
public function deposit($amount)
{
if ($amount > 0) {
$this->balance += $amount;
}
}
public function withdraw($amount)
{
if ($amount <= $this->balance) {
$this->balance -= $amount;
return true;
}
return false;
}
}
Code language: HTML, XML (xml)
The withdraw()
method checks the current balance.
If the balance is less than the withdrawal amount, the withdraw()
method returns false
.
Later, you’ll learn how to throw an exception instead. Otherwise, it deducts the withdrawal amount from the balance and returns true
.
Summary
- Objects have states and behaviors.
- A class is a blueprint for creating objects.
- Properties represent the object’s state, and methods represent the object’s behavior. Properties and methods have visibility.
- Use the
new
keyword to create an object from a class. - The
$this
variable references the current object of the class.