Rekayasa Perangkat Lunak · program dalam pemrograman web, terkadang kita menuliskan beberapa kode...

Preview:

Citation preview

Modular PHPAdam Hendra Brata

Pemrograman Web

Modular PHP Pengenalan Web Modular

Include dan Require

Pokok Bahasan

Modular Web

Pernahkah kita sadar dalam menulis kode program dalam pemrograman web, terkadang kita menuliskan beberapa kode program yang pada dasarnya sama atau memiliki fungsi yang mirip, namun kita tulis berkali – kali untuk pembuatan fungsional web tersebut

Modular Web

<?php

$host = '127.0.0.1'; // localhost$db = 'test';

$user = 'root';

$pass = ""; // the password is an empty string

if($con = mysqli_connect($host, $user, $pass)) {

mysqli_select_db($db, $con);

$sql = "..."; // put your query here...

$result = mysqli_query( $sql );

}

?>

Rewriting Code : Connect to Database

Bagaimanapun bentuknya, dia akan

tetap sama saja

Ingat dengan permainan Lego ?

Modular Web

Modular Web

Konsep modular pada pemrograman web padadasarnya adalah “memisah” komponen / kodeprogram ke dalam beberapa file / modul yang berbeda

Kemudian modul / file terpisah ini masing -masing bias jadi akan memiliki tanggungjawab yang berbeda

Modular Web

Dalam PHP konsep ini bisa diwujudkan denganmenggunakan :

include

include_once

require

require_once

Modular Web

Include Syntax : include 'filename' ;

Contoh :

Modular Web : Include

<?php

echo "<p>Copyright FILKOM UB</p>";

?>

footer.php

<html>

< body>

< h1>Welcome to my home page!</h1>

< p>Some text.</p>

<p>Some more text.</p>

<?php include 'footer.php';?>

< h2> Hello </h2>

< /body>

< /html>

index.php

Include Syntax : include_once 'filename' ;

Contoh :

Modular Web : Include_Once

<?php

echo "<p>Copyright FILKOM UB</p>";

?>

footer.php

<html>

< body>

< h1>Welcome to my home page!</h1>

< p>Some text.</p>

<p>Some more text.</p>

<?php include_once 'footer.php';?>

< h2> Hello </h2>

< /body>

< /html>

index.php

Coba jalankan kode dibawah ini denganinclude dan include_once, amati yang terjadi

Modular Web : Include Vs Include_Once

<?php

echo "<p>Copyright FILKOM UB</p>";

?>

footer.php

<html>

< body>

< h1>Welcome to my home page!</h1>

< p>Some text.</p>

<p>Some more text.</p>

<?php

for ($i=1; $i<=5 ; $i++)

{

include 'footer.php'; //jalankan kemudian

ganti dengan include_once

}

?>

< /body>

< /html>

index.php

The include_once function is exactly the same as the include function except it will limit the file to be used once.

Include Syntax : require 'filename' ;

Contoh :

Modular Web : Require

<?php

echo "<p>Copyright FILKOM UB</p>";

?>

footer.php

<html>

< body>

< h1>Welcome to my home page!</h1>

< p>Some text.</p>

<p>Some more text.</p>

<?php require 'footer.php';?>

< h2> Hello </h2>

< /body>

< /html>

index.php

Letakkan file footer di folder / alamat yang berbeda dengan index .php dan jalankan file index.php

Modular Web : Include Vs Require

<?php

echo "<p>Copyright FILKOM UB</p>";

?>

footer.php

<html>

< body>

< h1>Welcome to my home page!</h1>

< p>Some text.</p>

<p>Some more text.</p>

<?php require 'footer.php';?> //ganti dengan

< h2> Hello </h2> include

< /body>

< /html>

index.php

The require function acts just like the include function except if the file can not be found it will throw a PHP error.

Modular Web : Include Vs Require

And the conclusion is…..

Include Syntax : require_once 'filename' ;

Contoh :

Modular Web : Require_Once

<?php

echo "<p>Copyright FILKOM UB</p>";

?>

footer.php

<html>

< body>

< h1>Welcome to my home page!</h1>

< p>Some text.</p>

<p>Some more text.</p>

<?php require_once 'footer.php';?>

< h2> Hello </h2>

< /body>

< /html>

index.php

Modular Web : Require Vs Require_Once

require_once adalah fungsi yang menggabungkan sifat require daninclude_once

require_once dapat memastikan file yang dibutuhkan ada sebelum ditambahkan ke dalam file dan sebagai tambahan fungsi require_once jugaakan memastikan file yang akan ditambahkantersebut hanya bisa digunakan sekali saja dalam file yang ingin ditambahkan

Terimakasih dan Semoga

Bermanfaat ^^

Recommended