Updated April 5, 2023
Introduction to PHP object type
The following article provides an outline for PHP object type. An object is a datatype of Php which stores the data. It is an instance which is defined by a class. In order to create the object, we first need to define the class first, and then the ‘n’ number of objects can be created of that class. The objects inherit all the properties and behaviour of the class, but each object of the same class has its own different values and properties so that it can be manipulated independently. Objects also contain information on how to process the information. Objects in Php are created using the new keyword. Objects are also called as instances.
Syntax:
Below given is the basic syntax of object type declaration and calling a function with that object in Php:
<?php
// defining the php class
class class_name{
function func() {
…
…
}
}
//declaring the php object 'obj'
$obj = class_name;
$obj -> func();
?>
Different PHP object types
As we all know that variables hold the data of different data types. Every data type in Php has a specific role. Php supports 9 data types:
- Boolean
- Float
- Arrays
- Object
- Resource
- Null
- String
- Integer
- Null
For Object-Oriented Programming (OOPs), it is mandatory for any programmer to understand its basic concepts. These basic concepts include:
- Class
- Objects
- Methods
- Properties
First and foremost, the thing that we learn from the OOPs is the Class. Class is nothing but a blueprint. It defines the actual layout of the task that needs to be performed. For example, in order to find the area of geometric figures like square, rectangle, triangle, the class is ‘Figure’. Objects are the instances of the class that can store the value and functions of that class. One class can have many objects, and each object has its own properties and is independent of each other. In the above class ‘Figure’, objects of square, rectangle and triangle can be created separately, which have their own properties. Let us see the basic things when working with objects:
1. Creating an object in PHP
Once the creation of the class is done, objects of that class are created. There can be a single or multiple objects of a single class. Objects in Php are created using the ‘new; keyword. Below given is the basic example of creating objects of ‘square’ and ‘rectangle’ of a class ‘Figure’ in Php.
rect = new Figure();
squ = new FIgure();
We have created the two objects ‘rect’ and ‘squ’ for the square and rectangle, respectively, of the class ‘Figure’. These 2 objects are independent of each other and have their own specific properties.
2. Calling member function with the object
After creating the class and its objects, the next thing that is done is the calling of member functions with those created objects.
Below given is the basic way of calling the member function with the object:
rect -> getArea(20, 30);
squ -> getArea(20);
rect -> getParameter(20, 30);
squ -> getParameter(20);
In the above example, 2 parameterized methods, ‘getArea’ and ‘getParameter’, are created. In order to access those methods, objects created above for the rectangle ‘rect’ and square ‘squ’ are used with the ‘ -> ‘ operator. Different parameters 1 and 2 are passed in order to call different functions for the square and rectangle, respectively.
3. Calling constructor function with the object
Constructor functions are the type of functions in Php, which are called automatically when the object is created. Programmers can initialize things using the constructor. Php provides a function __construt() for defining the constructor. Parameters can be passed easily using the constructor.
Below given is the basic example of calling the constructor function in Php:
function __construct( $arg1, $arg2 ) {
$this->length = $length;
$this->breadth = $breadth;
}
The programmer needs not to set the value in a separate function. This thing can be done directly in the constructor at the time of object creation, similar to the one given below.
$rect = new Figure(20, 30);
$squ = new Figure(20, 20);
Instead of creating the method for setting the values, programmers can directly pass the arguments at the time of object creation. Like in the object, ‘rect’ values (20, 30) are passed directly in the constructor.
Examples of PHP object type
Given below are the examples of PHP object type:
Example #1
<!DOCTYPE html>
<html>
<body>
<?php
class Student {
public $name;
public $address;
//constructor for the values passed 'name' and 'address'
public function __construct($name, $address) {
$this->name = $name;
$this->address = $address;
}
//function 'display()' to print the values
public function display() {
echo "Student name is ".$this-> name;
echo "<br>";
echo "Student address is ".$this ->address;
}
}
//Object declaration 'stud_details'
$stud_details = new Student('Rahul Raj', 'Agra');
//calling the method 'display' using the object 'stud_details'
echo $stud_details -> display();
?>
</body>
</html>
Output:
Example #2
Code:
<!DOCTYPE html>
<html>
<body>
<?php
// defining the class 'Figure'
class Figure {
public $length;
public $breadth;
//defining the constructor using __construct() method
function __construct($length, $breadth) {
$this->length = $length;
$this->breadth = $breadth;
}
// defining the function 'getArea'
function getArea() {
return $this->length*$this->breadth;
}
//defining the function 'getParameter'
function getParameter() {
return (2*($this->length + $this->breadth));
}
}
//creating object 'rect' for rectangle and passing arguments in the constructor
$rect = new Figure(20,30);
$squ = new Figure(20, 20);
echo "Area of rectangle ";
//calling the member method 'getArea' using the object created
echo $rect->getArea();
echo "<br>";
echo "Parameter of rectangle ";
//calling the member method 'getParameter' using the object created
echo $rect->getParameter();
echo "<br>";
//calling the member method 'getArea' using the object created for 'squ' object
echo "Area of square ";
echo $squ ->getArea();
?>
</body>
</html>
Output:
Conclusion
The above description clearly shows what PHP object type is and how it is declared and used in PHP programs. All the methods, functions, class members in PHP are accessed using the object. A single class in PHP can have many objects, and each object has its own properties. Since understanding objects is an important topic, one needs to understand them carefully and deeply before using them in code.
Recommended Articles
This is a guide to the PHP object type. Here we discuss the introduction, different PHP object types along with examples, respectively. You may also have a look at the following articles to learn more –