In object oriented programming there is technique through which we divide our main program into separate files and each file is known as Class.

So class is defined as a collection of variables and functions. Where variables are the attributes or properties and functions are the methods/actions performed by that class.

In PHP we write the simple class as it begins with the keyword class followed by a name of class and then followed by the pair of curly braces which encloses the definition of variables and functions.

In PHP version<5.3 we used to define variable  using the keyword ‘var’. but in the latest version we define the variable simply by its name prefixed by ‘$’

Functions are declared as it begins with the keyword ‘function’  and then name of function as we can see it in above example.

For demonstration purpose why we use class now we take an example:

Consider a simple program index.php

output

 How to use Class

Now if we do the same task using class then we will make Class named CrossLanguage.php and declare the two variables $name1 and $name2

and our index.php class will be changed as the follow . here first we include the CrossLanguages.php so that we can make its object in index.php. Then we will make the instance of this class and assign the value to the variables $name1 and $name2.

now the output of index.php will be the same as previous

 How to use Method

Now if we do the same thing using method then the same code will become

CrossLanguage.php

Index.php file

and now the output of index.php will be the same as previous