PHP OOP - Classes and Objects
An object is an instance of a class, which serves as a template for other objects.
OOP Case
Let us suppose we have such a Fruit class. A Fruit can have attributes such as a name, colour, weight, and so on. To store the values of these properties, we can declare variables such as $name, $colour, and $weight.
They inherit the class properties and behaviours when they are generated as individual objects (apple, banana, etc.), but each object will have a unique set of attributes.
Create or Define a PHP Class
When defining a class, the class
keyword is used, followed by the class name and a set of curly braces({}). They contain all of its characteristics and methods :
Syntax :-
Example 1 :- There is a property ($name) in the Fruit class, as well as set_name()
and get_name()
for setting and retrieving $name, respectively :
name = $name;
}
function get_name() {
return $this->name;
}
}
?>
Note : Variables are referred to as properties in a class, and functions are referred to as methods!
A class can have number of properties and methods.
All names (class, property, method) are user defined.
Example 2 :- There are three properties ($name, $dept, and $salary) in the Employee class, as well as there are six methods for setting and retrieving, their values respectively.
name = $name;
}
function get_name() {
return $this->name;
}
function set_dept($dept) {
$this->dept = $dept;
}
function get_dept() {
return $this->dept;
}
function set_salary($salary) {
$this->salary = $salary;
}
function get_salary() {
return $this->salary;
}
}
?>
Example 3 :- There are two properties ($a and $b) in the Calculator class, as well as there are four methods for making operations like addition and subtraction, respectively.
a = $a;
}
function setB($b) {
$this->b = $b;
}
function addAB()
{
$c = $this->a + $this->b;
echo "Addition is : " . $c;
}
function subAB()
{
$c = $this->a - $this->b;
echo "Subtraction is : " . $c;
}
}
?>
Related Links
Create or Define Objects
Classes are meaningless without objects! A class can be used to create many objects. In addition to the properties and methods defined in the class, each object will have a unique set of values for each of its properties.
The new
keyword is used to create single object of a class.
Syntax:-
object_name = new class_name();
Example :- Creating 3 objects of Fruit class and Creating 2 objects of Employee class.
Access Properties and Methods of Class using Object
Now we can see, how to access a class member (propety and method) using an object.
Syntax :-
// Access a propety
object_name->property_name;
// Access a method
object_name->method_name();
Example :-
// $apple from Fruit class
$apple->set_name("apple"); // Calling Method
echo $apple->get_name(); // Calling Method
// $john from Employee class
$john->name = "john"; // Accessing Property
$john->dept = "sales"; // Accessing Property
$john->set_salary(12000); // Calling Method
echo $john->get_name(); // Calling Method
echo $john->dept; // Accessing Property
echo $john->salary; // Accessing Property
Now we will see a complete program for creating class and objects.
Example :- In the following example, we create a class "Car" with related properties and methods.
name = $name;
}
function setPrice($price){
$this->price = $price;
}
function setColorAndFuel($color, $fuel){
$this->color = $color;
$this->fuel = $fuel;
}
function getCarInfo(){
echo "Name = " . $this->name . "
";
echo "Price = " . $this->price . "
";
echo "Color = " . $this->color . "
";
echo "Fuel = " . $this->fuel . "
";
}
}
// We are going to create objects
$BMW = new Car();
$BMW->setName("BMW");
$BMW->setPrice(20000);
$BMW->setColorAndFuel("red", "Petrol");
$BMW->.getCarInfo();
$Volvo = new Car();
$Volvo->setName("Volvo");
$Volvo->setPrice(15000);
$Volvo->setColorAndFuel("black", "Diesel");
$Volvo->.getCarInfo();
?>
Output:-
Price = 20000
Color = red
Fuel = Petrol
Name = Volvo
Price = 15000
Color = black
Fuel = Diesel
Related Links
PHP $this Keyword
The $this
keyword is a readymade or built-in object for accessing class members (properties and methods) within inside a class.
It also called as current class object. Only inside methods is the $this
keyword available, which relates to the current object.
Note:- In PHP, All class members (except static members) are accessed by object inside or outside the class.