본문 바로가기
PHP

PHP 파일로 모듈화

by abkorc33 2023. 1. 24.

리팩토링
파일로 모듈화 - require

<?php
require('lib/print.php');
?>
<?php
require('view/top.php');
?>

 

함수 및 view 라이브러리 화

 

php에서 한번 만들어진 함수는 재정의 할 수 없다. -> 재 호출 되면 오류 발생


require_once('lib/print.php');
한번 이미 호출 했기때문에 _once를 붙이면 php가 무시한다.

 

print.php (중복으로 등장하는 함수 부분 파일 모듈화)

<?php
function print_title() {
    if(isset($_GET['id'])) {
        echo $_GET['id'];
    } else {
        echo "Welcome";
    }
}
function print_description() {
    if(isset($_GET['id'])) {
        echo file_get_contents("data/".$_GET['id']);
    } else {
        echo "Hello, PHP";
    }
}
function print_list() {
    $list = scandir('./data');
    $i = 0;
    while($i < count($list)) {
        if($list[$i] != ".") {
            if($list[$i] != "..") {
                echo "<li><a href='index.php?id=$list[$i]'>$list[$i]</a></li>\n";
            }
        }
        $i++;
    }
}
?>

top.php (중복으로 등장하는 html코드 파일 모듈화)

함수가 들어있는 파일을 호출하지 않아도 정상 작동하지만

함수를 호출하는데 어디서 호출하는지 top.php에서 알지 못한다

require_once로 호출해주면 다른 파일에서 중복 호출되더라도 무시함

<?php require_once('lib/print.php'); ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            <?php
            print_title();
            ?>
        </title>
    </head>
    <body>
        <h1><a href="index.php">WEB</a></h1>
        <ol>
            <?php
            print_list();
            ?>
        </ol>

bottom.php (중복 등장 태그/ 2줄코드 파일 모듈화 비효율적)

</body>
</html>

 

✔️리팩토링

 

index.php

<?php
require_once('lib/print.php');
require_once('view/top.php');
?>
        <a href="create.php">create</a>
        <?php
        if(isset($_GET['id'])) { ?>
            <a href="update.php?id=<?=$_GET['id']?>">update</a>
            <form action="delete_process.php" method="POST">
                <input type="hidden" name="id" value="<?=$_GET['id']?>">
                <input type="submit" value="delete">
            </form>
        <?php
        }
        ?>
        <h2>
            <?php
            print_title();
            ?>
        </h2>
            <?php
            print_description();
            ?>
<?php require_once('view/bottom.php'); ?>

create.php

<?php
require_once('lib/print.php');
require_once('view/top.php');
?>
        <a href="create.php">create</a>
        <form action="create_process.php" method="POST">
            <p>
                <input type="text" name="title" placeholder="Title">
            </p>
            <p>
                <textarea name="description" placeholder="Description"></textarea>
            </p>
            <p>
                <input type="submit">
            </p>
        </form>
<?php require_once('view/bottom.php'); ?>

update.php

<?php
require_once('lib/print.php');
require_once('view/top.php');
?>
        <a href="create.php">create</a>
        <?php
        if(isset($_GET['id'])) { ?>
            <a href="update.php?id=<?=$_GET['id']?>">update</a>
        <?php
        }
        ?>
        <h2>
            <?php
            print_title();
            ?>
        </h2>
            <?php
            print_description();
            ?>
        <form action="update_process.php" method="POST">
            <input type="hidden" name="old_title" value="<?=$_GET['id']?>">
            <p>
                <input type="text" name="title" placeholder="Title" value="<?php print_title(); ?>">
            </p>
            <p>
                <textarea name="description" placeholder="Description"><?php print_description(); ?></textarea>
            </p>
            <p>
                <input type="submit">
            </p>
        </form>
<?php require_once('view/bottom.php'); ?>

'PHP' 카테고리의 다른 글

[정규식] preg_match("/(http:)/i", $test)  (0) 2023.10.18
PHP 보안 파일 경로 보호  (0) 2023.01.24
PHP CRUD 기초  (0) 2023.01.24
PHP 기초4  (0) 2023.01.20
PHP 기초3  (0) 2023.01.20

댓글