How to create classes and objects?
In this section we will discuss about some basic aspect of oop in php. For every basic aspect we have separate chapter in this tutorial. If you will say basic aspect of oop in php then it is all about classes, objects. For the further detail of oop topic like interface, object cloning etc then you can go to specific chapter of this tutorial. Refer table of contents for the complete list of chapter and its navigation. So let us discuss about basic concept of class, object inheritance here.
Class in PHP
Class is a blueprint of any object in oop. So class is the first alphabet of oop. In php you can creation of class is very simple. You can create class using tag class. In class block you can define your properties as class variable and function as class behavior. So let us create a class for interest calculator and define its properties like rate, capital, duration and behavior like calculate interest.
1 2 3 4 5 6 7 8 9 10 |
class interestCalculator { var $rate; var $duration; var $capital; function calculateInterest() { return ($this->rate*$this->duration*$->capital)/100; } } |
Above is a very simple and basic class to calculate interest. Let us explore all basic aspect of this class.
You can create class in php by using class keyword. here class interestCalculator{ }
is class block. You can define all of your properties and methods(behavior of class, we will use method or function instead of behavior) of your class inside of your class block. All variable started with var keyword is property of your class. Commonalty we can say these are variable of class also. And the function are methods of this class. You can design your own class with your won variable and function.
Object in PHP
As we have already discussed that object is an instance of any class. So we will take our interestCalculator class as an example. Creating object of the class is very easy in php. You can create object of class with the help of new keyword. Following is very basic example of creation of object of your class interest calculator:
1 |
$calculator = new interestCalculator() |
In above declaration you are creating object of your class interestCalculator in variable $calculator. Now your variable $calculator is an object of class interestCalculator. Next step is to set property or variable of object calculator and perform calculation of interest.
1 2 3 4 5 |
$calculator = new InterestCalculator() $calculator->rate = 3; $calculator->duration =2; $calculator->capital = 300; echo $calculator->calculateInterest(); |
Here object of your class interestCalculator is your php variable $calculator. In next 3 lines of above code you are setting properties of class. You can access property of class with ->. So in above code rate property is set using $calculator->rate = 3;
. Finally after setting all reaqired properties you have called method calculateInterest.
Hope you have clear understanding of oop in php. Download the basic code and run at your machine.