Write your first Class on PHP

Hello friends. Hope you already learn very strongly about the object oriented programming and different between class and object. Now, I want to tell details about the class. The basic syntax of PHP Class is –

class CLASS_NAME {


// Some properties and method


// some properties and methods


OK. This is the basic syntax of class. Lets to describe.

1. Every class start with class key word before class name

2. class name is like a variable. We can choose anything for class name

3. we can use some property and method in both private and public.

Now, lets make our first class on my address.

class myAddress{


$name=”Bismillahir Rahmanir Rahim”;

function get_name()


return $this->$name.'<br>’;


function set_name($newValu)





$obj=new myAddress;

echo $obj->get_name();

$obj->set_name(“Allah is Almighty”);

echo $obj->get_name();

$obj->set_name(“Muhammad (SM) is the messenger and Prophet of Allah”);

echo $obj->get_name();

OK. Now save this file in the name of test.php  and run this file in your local server. You will see like this output Insallah.

Bismillahir Rahmanir Rahim

Allah is Almighty

Muhammad (SM) is the messenger and Prophet of Allah

Ok. It is the very basic class on PHP. Thanks for read this article.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s