PHP Object Oriented Programming

Programming, in simple words, means giving instructions to a computer to process the data and provides the required output. There are mainly two programming approaches, Procedure Oriented Programming (POP) and Object Oriented Programming (OOP).

In procedure Oriented Programming, you first break the problem into smaller sections of code and then solved each section separately. Later on, all the solved sections of the program are integrated to solve the entire problem. Each small section of the code is written within a block of code, called a method. You can call one method from another. Therefore, these methods are dependent on each other. As a result, re-usability of the applications becomes difficult.

The concept of object-oriented programming revolves entirely around objects and their interaction to design applications and computer programs.

The concept of object-oriented programming has been introduced to overcome the difficulty of limited or no re-usability of code. It also adopts the approach of combining the independent object, which interact with each other to solve the overall problems.

Amongst these two approaches, OOP is considered as the best one, since it follows a model related to real world objects.

OOPS ADVANTAGES:

There are various advantages of using object-oriented programming.

RE-USABILITY OF CODE: In POP methods are dependent on each other, but if you will use the OOP concept greater re-usability of code maintains in a programming.

EASY TO HANDLE: Object-oriented programming techniques are easier to handle. We used class and object concept in OOP.

GREAT LEVEL OF ABSTRACTION: In OOP we used an approach of combining the independent object, which interact with each other to solve the overall problems and execute the business logic of the program.

How OOP Work?

The basic concept of object oriented programming in PHP then it is classes and objects.

CLASS is very easy to define and create in PHP. We used tag class.

For Example:

In this example, we create a class Rectangular_Solid_Volume. We create a function calculateVolume () to calculate the volume of rectangular solids. We used the formula length * width * height.

<?php

class all_languages

{

public  $great_language=‘php’;

}

//use ‘new’ keyword

$lang = new all_languages;

echo ‘ I like ‘. $lang->great_language.’.’

?>

Output:

php-object1

Now we create another example for class and object using $this variable.

$this variable is a special variable; it refers itself (means same object).

Php code:

<?php

class language_list

{

// Member variables are defined

var $lang_name;

// Member functions

function set_lang_description($value)

{

$this->lang_name = $value;

}

function get_lang_description(){

echo $this->lang_name <br/>;

}

}

$html = new language_list;

$php = new language_list;

$asp = new language_list;

// calling Member functions

$html->set_lang_description( HTML:Hypertext Markup Language” );

$php->set_lang_description( PHP:Hypertext Preprocessor” );

$asp->set_lang_description( “ASP:Active Server Page” );

$html->get_lang_description();

$php->get_lang_description();

$asp->get_lang_description(); 

?>

Output:

php-object2


Leave a Reply

avatar
  Subscribe  
Notify of