- PHP OOP — Classes and Objects
- OOP Case
- Define a Class
- Syntax
- Define Objects
- Example
- Example
- PHP — The $this Keyword
- Example
- Example
- Example
- PHP — instanceof
- Classes and Objects in PHP Examples
- Class:
- Objects:
- Example-1: Declare and read class properties
- Example-2: Declare a class with properties and method
- Example-3: Declare a class with properties and method with an argument
- Example-4: Initialize the class properties outside the class
- Video Tutorial
- Conclusion:
- About the author
- Fahmida Yesmin
PHP OOP — Classes and Objects
A class is a template for objects, and an object is an instance of class.
OOP Case
Let’s assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.
When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Define a Class
A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces (<>). All its properties and methods go inside the braces:
Syntax
Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property:
class Fruit // Properties
public $name;
public $color;
?php
// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
>
?>
Note: In a class, variables are called properties and functions are called methods!
Define Objects
Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.
Objects of a class are created using the new keyword.
In the example below, $apple and $banana are instances of the class Fruit:
Example
class Fruit // Properties
public $name;
public $color;
?php
// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
>
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name(‘Apple’);
$banana->set_name(‘Banana’);
echo $apple->get_name();
echo «
«;
echo $banana->get_name();
?>
In the example below, we add two more methods to class Fruit, for setting and getting the $color property:
Example
class Fruit // Properties
public $name;
public $color;
?php
// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
function set_color($color) $this->color = $color;
>
function get_color() return $this->color;
>
>
$apple = new Fruit();
$apple->set_name(‘Apple’);
$apple->set_color(‘Red’);
echo «Name: » . $apple->get_name();
echo «
«;
echo «Color: » . $apple->get_color();
?>
PHP — The $this Keyword
The $this keyword refers to the current object, and is only available inside methods.
Look at the following example:
Example
So, where can we change the value of the $name property? There are two ways:
1. Inside the class (by adding a set_name() method and use $this):
Example
class Fruit public $name;
function set_name($name) $this->name = $name;
>
>
$apple = new Fruit();
$apple->set_name(«Apple»);
?php
2. Outside the class (by directly changing the property value):
Example
class Fruit public $name;
>
$apple = new Fruit();
$apple->name = «Apple»;
?php
PHP — instanceof
You can use the instanceof keyword to check if an object belongs to a specific class:
Classes and Objects in PHP Examples
Any complex application can be developed in a more manageable and maintainable way by using object-oriented programming (OOP). It is more efficient than procedural programming for developing large and complicated applications. In this programming, all variables and functions are defined as a group by using class and the instance of a class is called an object that is used to access the properties of the class. This tutorial shows the basics of object-oriented programming with the uses of class and object.
Class:
Each class contains the required variables and functions to define the properties of a particular group. Generally, the name of the class is defined by starting with the capital letter and in the singular form. The keyword, the class is used to declare a class.
Objects:
The object is declared to use the properties of a class. The object variable is declared by using the new keyword followed by the class name. Multiple object variables can be declared for a class. The object variables are work as a reference variable. So, if the property value of any class is modified by one object then the property value of another object of the same class will be changed at a time.
Example-1: Declare and read class properties
The following example shows the way to declare and access the properties of a class. Create a PHP file with the following script. Two properties named $name and $price of the class named Product are declared and initialized with the values. Next, an object of this class is declared to print the values of the properties as an object and print each property value separately.
//Print all object properties
//Print each property separately
echo «
Product Name: » . $obj_pro -> name . «
» ;
echo «Product Price: » . $obj_pro -> price . «
» ;
The following output will appear after running the above script from the server.
Example-2: Declare a class with properties and method
The following example shows the way to declare the property and method in a class. Create a PHP file with the following script. $name, $type and $price have declared as properties of the class named Product. A function named details() has been declared as the method of the class that will print the property values of the class. Next, an object of this class has declared and called the method, details().
public $name = «HP Pavillion» ;
//Declare method to print the properties
echo «Name :» . $this -> name . «
» . «Type :» . $this -> type . «
» . «Price :$» . $this -> price . «
» ;
The following output will appear after running the above script from the server.
Example-3: Declare a class with properties and method with an argument
The following example shows the use of the property and the method with an argument in a class. Create a PHP file with the following script. Three property values named $name, $type, and $price have been declared and initialized with the values. A function named total_price() has been declared with an argument named $qty as the argument. total_price() will calculate the total price of the product based on the argument value and return it to the caller. Here, $this variable is used to read the value of the class property, $price. Next, an object variable named $object has been declared to access the property and method of the class. $quantity variable has been used in the script to pass the argument value to total_price(). All property values and the return value of the function will be printed by using an object variable.
public $name = «HP Pavillion» ;
/*Declare method with argument to calculate
the total price and return*/
public function total_price ( $qty )
//Calculate the total price
$total_price = $object -> total_price ( $quantity ) ;
//Print the product details with total price
echo «Name : » . $object -> name . «
» .
«Únit Price : $» . $object -> price . «
» .
«Total Price : $» . $total_price ;
The following output will appear after running the above script from the server.
Example-4: Initialize the class properties outside the class
In the previous examples, all property values are initialized inside the class. The following example shows how the class properties will be initialized by using the object of the class. Create a PHP file with the following script. Here, three class properties have been defined inside the class without initialization. Next, an object variable is used to initialize the class properties and print property values.
//Declare properties without values
//Initialize the property values
$object -> name = «Samsung Printer M06753» ;
//Print the property values
echo «Name :» . $object -> name . «
» . «Type :» . $object -> type . «
» . «Price :$» . $object -> price . «
» ;
The following output will appear after running the above script from the server.
Video Tutorial
Conclusion:
Class and object are the basic part of object-oriented programming. The concept of the class property and the method are to be cleared to learn object-oriented programming. The basic concept of the class and object have explained in this tutorial. How the property and the method with argument are declared in a class, how the property value can be initialized inside and outside the class and how the object variable can be used to access the property and method of the class have shown here by using different examples.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.