PHP OOPS Tutorial
Whatever we see around is an object, the computer we use, the pen, the book and the television—all of them are objects—objects define our lives. We need to have objects around us for leading lives in a much easier and convenient way. Objects are real world entities and they have the problem solving capacity.
In programming real world objects are used specially with the help of a different programming language, referred to as Object Oriented Programming. OOPS is all about real world identities—objects—students, employees and cars are real world employees. This programming language helps us in creating and understanding programs on real world entities.
In PHP OOPS plays an important role, given websites are designed to interact with humans. The humans that interact with the site can be a student, employee or a faculty.
We bring to you a Tutorial on PHP OOPS. Here is all you need to know about Object Oriented Programming in PHP: –
Class:
It is defined by a programmer. It is a data-type and it represents the similar kind of data in the program. The class can also be considered as a template for many multitude designs based on a same class.
<?php
class phpClass{
var $var1;
var $var2 = “constant string”;
function myfunc ($arg1, $arg2) {
[..]
}
[..]
}
?>
That is how you define a class. The third line has all the names of the variables declared Var1, Var2. These will be repeatedly used in the program wherever necessary.
Object:
An object is one instance of a class. It can be like salary of an employee or result of a student. A class can only be made once but then you have the liberty to create as much objects you want to create. Objects run the programming and they make the programs real. They are also known as instances at various places.
physics = new Subject;
$maths = new Subject;
$chemistry = new Subject;
This is how an object is defined in an Object Oriented Programming in PHP.
Member Functions:
Like all other functions they are also used to transfer control. They are defined in an object and are used for accessing data. These member functions are important for the making the program run with ease and grace.
physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );
You can see here Physics, Chemistry and the Maths are the objects and they are called by their member functions.
Polymorphism:
Polymorphism is a special concept of Object Oriented Programming. This feature allows the user and the programmer to use the same function at different places for various uses. The function name remains the same but can be brought under with different arguments. This is specifically related to OOPS. Developers make use of polymorphism with great excitement because it allows them to cut short their program and make it more efficient.
Constructor:
It refers to the type of function which gets automatically called wherever a new object is made or declared.
Destructors:
It refers to the type of function which gets automatically called wherever a new object is deleted or discontinued.