Basics Of OOPs in PHP
Object-oriented programming (OOP) was first introduced in php4. The area for oop in PHP version 4 was not very vast. There were only few features available in php4. The major concept of object-oriented programming in PHP is introduced in version 5(we commonly know as php5). Also, PHP community has plan to modify its object model structure in more better manner in php6(not released yet). But still in php5 object model is designed nicely. If you have a good understanding of OOP then you can create a very good architecture for your PHP application. You only need to know some of the basic principles of object-oriented programming and how to implement that concept of oop in PHP. In whole series I will use the abbreviation OOP for Object Oriented Programming.
PHP Class and Objects
Objects
Anything is the world is an object. Look around and you can find lots of object. Your laptop, pc, car every thing is an object. In this world every object has two thing properties and behaviors. Your car has property (color, brand name) and behavior(it can go forward and backward). If you are able to find properties and behaviors of real object. Then it will be very easy for you to work with Object Oriented Programming.
In real world different objects has different properties and behaviors. For example your television has property size, color, and has behavior turn on, turn off. If you observe carefully then you can find that every object has some property and behavior from other object. This phenomena is called inheritance. For example car object has property and behavior from engine object.
If you are able to understand what is object then good to go ahead. If not then please start visualizing properties and behavior of object around you. You will defiantly understand.
Object in programming is similar to real word object. Every programming object has some properties and behaviors. For example if you have object for interest calculator then it has property interest rate and capital and behavior simple interest calculation and compound interest calculation. Interest calculator has some property and method from calculator object like addition, multiplication etc.
What is Class?
Class is something which defines your object. For example your class is Car. And your Honda car is object of car class. Like object explanation, here we will take an example of the real word and then we will move further in programming definition.
Blueprint of the object is class. Class represents all properties and behaviors of object. For example your car class will define that car should have color, number of door and your car which is an object will have color green and 2 doors. Your car is object of class car. Or in terms of programming we can say your car object is an instance of the car class. So structural representation (blueprint) of your object is class.
Now let us take an example of the programming. Your interest calculator object is instance of class interest calculator. Interest calculator class defines properties like capital rate, and behavior like simple interest calculation and compound interest calculation. Your interest calculator object has property rate as 3% and capital 300 USD. So you are describing your class definition of rate by giving rate value equals to 3% and capital 300USD in your interest calculator object. Now in your object when interest calculation behavior will be applied it will take your rate of interest and capital and provide you the result. Again your interest calculator class will inherit the definition of its property and behavior from calculator class.
Example for class and Object(How to add two numbers)
1 2 3 4 5 6 7 8 9 |
class addnumbers { var $num1; var $num2; function addtwono() { return ($this->num1*$this->num2); } } |
In the above code we can create a class by using class keyword. Here class addnumbers is a class. All the variable start with var keyword is the propert of class. In other word these are variables of the class. we can design own class and function.
Now we can create the object of the class by using new keyowrd.
Example of creation of object of the class addnumbers
1 |
$add = new addnumbers(); |
$add variable is the object of addnumber class. Now we can set the properties of variables and perform addition of two numbers.
1 2 3 4 |
$add = new addnumbers(); $add->num1=5; $add->num2=6; echo $add->addtwono(); |
Output : 11