Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update and expand it more easily. Here, you will see a class and an object with a real example in PHP.
Table of Content
In this article, we will cover the following topics:
- What a class is and how it works.
- What an object is and how to create an instance from a class in PHP.
- Examples.

Let’s start with the definition and purposes.
What is a Class in PHP?
A class in PHP is a template that helps developers create objects. It defines variables (called properties) and functions (called methods) that the objects will use.
Properties may be static and should have access modifiers to secure their data. Methods should also have access modifiers and may be static.
Here is an example:
class Languages {
public $name;
public $country;
public function setName($n) {
$this->name= $n;
}
public function setCountry($c){
$this->country= $c;
}
}This class is called Languages. The code includes two variables: $name for the language name and $country for the country where people speak it.
It also contains two functions. The setName() function saves a language name, and the setCountry() function saves a country name.
You can use these functions to set values for the object’s variables when you create an object from this class.
Let’s move on to the following section to see how to get an instance of this class.
What is an Object in PHP?
An object in PHP is an instance that originates from a class. The class is the plan, and the object is the real thing you can use. Each object has its own data and can use the functions from the class.
We will proceed with the previous example. Let’s create an instance of the Languages class.
$lang = new Languages();
// Add the language
$lang->setName("Japanese");
// Add the country name
$lang->setCountry("Japan");
// Print result
echo "{$lang->country} is in Asia, and their mother tongue is {$lang->name}.";The output:
Japan is in Asia, and their mother tongue is Japanese.
Here is the full example:
class Languages {
public $name;
public $country;
public function setName($n) {
$this->name= $n;
}
public function setCountry($c){
$this->country= $c;
}
}
$lang = new Languages();
$lang->setName("Japanese");
$lang->setCountry("Japan");
echo "{$lang->country} is in Asia, and their mother tongue is {$lang->name}.";Let’s see more examples:
Examples of Class and Object in PHP
Example 1: This class stores the name of a sport and the number of players required.
class Sport {
public $name;
public $players;
public function setSport($n, $p) {
$this->name = $n;
$this->players = $p;
}
public function getSportInfo() {
return "The sport {$this->name} needs {$this->players} players.";
}
}
$sport1 = new Sport();
$sport1->setSport("Football", 11);
echo $sport1->getSportInfo();Here is the output:
The sport Football needs 11 players.
How it works:
- The class Sport defines two variables:
$namestores the sport’s name.$playersstores the number of players.
- The setSport() method assigns values to these variables.
- It takes two inputs:
$nfor the name and$pfor the number of players. - It stores them in the class properties
$nameand$players.
- It takes two inputs:
- The getSportInfo() method returns a sentence with the sport’s details.
Example 2: This stores clothing type and color in a class.
class Fashion {
public $type;
public $color;
public function setClothing($t, $c) {
$this->type = $t;
$this->color = $c;
}
public function getClothingInfo() {
return "This {$this->color} {$this->type} looks stylish.";
}
}
$shirt = new Fashion();
$shirt->setClothing("shirt", "blue");
echo $shirt->getClothingInfo();The output:
This blue shirt looks stylish.
The Fashion class has two properties: $type for the clothing type and $color for its color.
The setClothing() method takes two inputs, $t for type and $c for color, then assigns them to the class properties.
The getClothingInfo() method returns a sentence that describes the clothing item.
Example 3: This class stores a natural element and its description.
class Nature {
public $element;
public $description;
public function setElement($e, $d) {
$this->element = $e;
$this->description = $d;
}
public function getElementInfo() {
return "{$this->element} is {$this->description}.";
}
}
$sea = new Nature();
$sea->setElement("Sea", "vast and deep");
echo $sea->getElementInfo(); Here is the output:
Sea is vast and deep.
The Nature class stores information about natural elements. It has two properties: $element, which holds the name (e.g., sea, earth), and $description, which describes it.
The setElement() method assigns values to these properties with take two inputs: $e for the element name and $d for its description.
It then stores them in $element and $description. The getElementInfo() method returns a sentence describing the element.
Wrapping Up
You learned how to use a class and an object with a real example in OOP PHP. Here is a quick recap:
A class is a template that defines variables (properties) and functions (methods). An object is a real instance of a class that holds data and performs actions using the class’s methods.
Key Takeaways:
- A class is a group of related properties and methods.
- An object comes from a class and stores its own values.
- You create an object with the
newkeyword. - Objects can call methods to set and get data.
FAQ’s
Why use a class in PHP?
What is a class and object with an example?
class Animal {
public $type;
public function setName($n) {
$this->type = $n;
}
}
$myAnimal = new Animal();
$myAnimal->setName("Lion");
Here, Animal is the class and $myAnimal is an object.
What is an object in PHP?
Why use a class in PHP?
Similar Reads
The PHP superglobal $_FILES is a very important utility and great for dealing with file uploads on the web. When a user…
It’s very important to remember user data for each session when building web applications. This enables a high level of…
Sometimes things go wrong when PHP runs the code. So, you need to know the reasons for this error to…
PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…
So, what is a function? Quite simply, a function in PHP is a set of instructions you write once, and…
PHP has a number of predefined constants that give you some information about the language and its environment. You can…
The PHP associative array is a structured collection wherein data elements are organized into lists or groups. Each element is…
Developers needed a way to check the length of a string, so the strlen function was introduced to handle this…
The filter_var_array() appeared to make input validation simple and sanitization in PHP. It handles multiple inputs with different filters was…
The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…