Object Oriented PHP!!
Here I am with another tutorial and as requested by goyal hope you guys get benefited by this....
NOTE: This tutorial is copyrighted to CE as its my own work!! 😛
Before getting into OO programming we have to know what are the basic features available in object oriented programming. So lets begin with features in OO programming. following is the list of important features when it comes to OO programming:
NOTE: I know you guys already read and know about it but still I thought may be I can add my thought and understanding on these features 😀
Objects: It's a real time entity it can be a name, person or a place. technically speaking its defined as the instance of a class.
Class: Collection of similar objects together is called a class.
Inheritance: The ability of a program to be reused basically re-usability of code, which is once written and can be used again and again
Polymorphism: The ability of a program or a function which can act differently at different instances.
NOTE: The above features holds fine for all the programming languages which supports object orient programming 😉
Let's start by defining a class in PHP....
In order to define a class in php we have to use the keyword class, a typical class structure will be as shown below:
class class_name
{
var variable1;
function functionname(argument list)
{
//Some commands
}
}
Looks simple right!! yes it is easy ok now lets try something interesting... using class in php. I am going to define a class named crazyengineers and define a variable and then define two functions within the class so lets do it,
<?php
class crazyengineers
{
var $name;
function Greet()
{
echo "Welcome to CE!!";
}
function Pride()
{
echo "Be proud to be a CEAN 😀";
}
}
?>
Ok So what we have done is... we have defined a class named crazyengineers using the keyword class and I have defined a variable named $name which does not hold any value (just a variable declaration) and then I have defined two functions Greet() and Pride() which is going to display the message "Welcome to CE!!" and Be proud to be a CEAN 😀 " respectively. if you probably execute the above code you will not get any output because we have just defined a class with two function but we have not called it any where in the program, so unless we call the function its not gonna do the command what we want it to do...
So in order to access the elements ($name, Greet() and Pride() are the elements of the class crazzyengineers) in a class we require an object so lets create one. In order to create a object in class we use the keyword new followed by the class name, following is the object for the class crazyengineers:
$ceobject = new crazyengineers;
So I have created an object named $ceobject for the class called crazyengineers . That's it!! now how can we use the object to access the class methods or variables?? that's again easy Lets see how to do it for instance I wanna access the variable $name then I have to do something like this,
$ceobject->name;
Yeap the object name followed by the -> operator and the variable name with out the prefix '$' will allow you to access the variable $name in the class crazyengineers using its object $ceobject.
Ok how can I assign a value to that variable using an class object?? simple...
$ceobject->name='slashfear';
Now I have assigned a value slashfear for the variable named $name which is in the class crazyengineers. Accessing the function in the class works the same way,
$ceobject->Greet();
$ceobject->Pride();
Now lets complete our code:
<?php
class crazyengineers
{
var $name;
function Greet()
{
echo "Welcome to CE!!";
}
function Pride()
{
echo "Be proud to be a CEAN 😀";
}
}
$ceobject = new crazyengineers;
$ceobject->name = 'slashfear';
echo "Hi " . $ceobject->name;
echo "<br>";
$ceobject->Greet();
echo "<br>";
$ceobject->Pride();
?>
It's done!! Output of the above program will be as shown below:
Hi slashfear
Welcome to CE!!
Be proud to be a CEAN 😀
I guess you guys understood about the basics of class and objects in PHP.
If you have any doubts please feel free to ask!! Love to solve you doubts and also post in your feedback's (I have spent time in writing this tutorial for you guys!!)
-Arvind