Object Oriented PHP!!

Hi Ceans,

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,

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:



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 "
";

$ceobject->Greet();

echo "
";

$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

Replies

  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    Better than mine, buddy! Good going... ๐Ÿ˜€ Thanx... ๐Ÿ˜€

    But, it would be better, if you enclose those yellow stuff inside PHP tags. ๐Ÿ˜€
  • Ankita Katdare
    Ankita Katdare
    @slashfear: Stupendo-fantabulously-fantastical! ๐Ÿ‘
    Thank you so much for putting in so much of time in this. Your tutorials are great.
  • Manish Goyal
    Manish Goyal
    very nice

    Just a suggestion How about putting these valuable threads in a separate section, so that these threads will not be lost somewhere between other 34000 threads
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    goyal420
    very nice

    Just a suggestion How about putting these valuable threads in a separate section, so that these threads will not be lost somewhere between other 34000 threads
    Can we have a new subforum as tutorials? This is what I asked the admins.
  • slashfear
    slashfear
    Thanks for the reply guys !! More tutorials coming soon...... (If you have any special topic you want tutorial on please post in here or PM me ๐Ÿ˜‰ if I know about it I will surely write a tutorial on it for you guys...)


    -Arvind

You are reading an archived discussion.

Related Posts

My friend is looking for an Android phone and his budget is Rs. 20k. He's keen on getting an Android and wants a good quality camera. Post your suggestions ๐Ÿ˜€
1. What are the reasons for bed height variation in CFBC boilers. 2. Whether auto-control is possible? 3. Why furnace pressure should reach high (> 250mmWc)?
Is furnace draft auto-control possible in CFBC boilers?
And Please also Explain which is the best of the above mentioned ? pleas help me it is important for me.Because its about my career . I m going to...
Hello I am new to the Visual Studio C++ i cant the book Visual C++ programming by yashwant Kanetkar , if any one have the link to download or study...