Modular code in PHP

Modular code in PHP

Modular code

Modular code is a small piece of code that perform separate task and we can use again and again, this type of code is called modular code. Pearl package is the good example of modular code. There are many pearl packages in word that have thousands of modular code.

Why used modular code?

Code reusability: – The main advantage of modular code is code reusability, write once use many times in all application and you can also make code library and use this modular code in lot of application by including this library.
If you want to learn how include function works visit include funtion.

Ease of maintenance: – Suppose you have ten copy of same code in ten different places of your application and you want to fix bugs or enhance the functionality of code then you have to change this code in all page. If you are using modular code you can change one place and all places code has changed.

Readability: – You can increase the efficiency of your program by using modular code. If you divide your program into lots of modular code then obviously readability increases.

Efficiency: – Modular code increase the efficiency of you application. Suppose you have big PHP file that have a lot of PHP code and you want to access only array sorting code. If you using only sorting of array code is more efficient than using hole code that written in a PHP file.

PHP contain a lot of feature to implement the modular code.

Function

You can write piece of code in a function and use again and again by calling the function in anywhere of our application. I already told you, you can make function library to use this modular code in another applications.

Syntax

function function_name()

{

Your code;

}

Example


<?php

function test()
{
echo "Hello, world! <br/> ";

echo "welcome to php";
}

test(); //you can call any number of times 
test(); // like that
test();

?>

Output

Hello, world!
welcome to phpHello, world!
welcome to phpHello, world!
welcome to php

Classes

Classes contain both property and function within a single class that also enhance the security of application.

Syntax of class

class class_name 

{
Property; 
Behaviour;
}

Example

<?php

class Car 
{
public $color;
public $manufacturer;
}
$first = new Car();
$first -> color = "blue";
$first -> manufacturer = "Audi";
$second = new Car();
$second -> color = "yellow";
$second -> manufacturer = "bmw";
echo " Some properties: ";
echo " <p> The Beetle's color is " . $first -> color . ". </p> ";
echo " <p> The Mustang's manufacturer is " . $second -> manufacturer . ". </p> ";
echo " The \$first Object: <pre> ";
print_r( $first );
echo " </pre> ";
echo " The \$second Object: <pre> ";
print_r( $second );
echo " </pre>";
?>

Output

Some properties:

The Beetle’s color is blue.

The Mustang’s manufacturer is bmw.

The $first Object:

Car Object
(
[color] => blue
[manufacturer] => Audi
)

The $second Object:

Car Object
(
[color] => yellow
[manufacturer] => bmw
)

Include () and require ()

You can include the piece of code from one script to another scrip by using these functions.

Namespaces

These are useful for large applications and libraries. They help to avoid clashes where the same class name, function name, or constant name is accidentally used in different libraries, or in different parts of an application

If you want to learn more about functions, classes, include, and function library please visit
php tutorial



The following two tabs change content below.
Umar Farooque Khan is Software developer and professional blogger serving Worldwide on freelancer and ODesk platform to show the capability of his skills. He is the founder of pTutorial.com. The main purpose of this site is sharing and exchanging of Knowledge related to the recent technologies.
This entry was posted in PHP on by .

About Umar Farooque Khan

Umar Farooque Khan is Software developer and professional blogger serving Worldwide on freelancer and ODesk platform to show the capability of his skills. He is the founder of pTutorial.com. The main purpose of this site is sharing and exchanging of Knowledge related to the recent technologies.