Thuta Learning
PHP
AdvancedProgrammingအခြေခံ

Include & Require

PHPသင်ခန်းစာ 2130 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • include, require နဲ့ _once ပုံစံတွေရဲ့ ကွာခြားချက်ကို ဖိုင်ပျောက်တဲ့အခါ၊ ထပ်တင်တဲ့အခါ အပြုအမူအလိုက် ရှင်းပြနိုင်ဖို့
  • Included file က ခေါ်တဲ့ scope ထဲမှာ run တာနဲ့ return value ပြန်ပေးနိုင်တာကို config file တစ်ခုမှာ သုံးနိုင်ဖို့
  • __DIR__ နဲ့ path ကို ဖိုင်ရဲ့ ကိုယ်ပိုင်တည်နေရာကနေ တည်ဆောက်ပြီး working directory ပေါ် မမှီခိုနိုင်ဖို့
  • User ရွေးတဲ့ path ကို include မလုပ်ဘဲ allow-list နဲ့ လုံခြုံစွာ ရွေးခိုင်းနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Include နဲ့ require ကို ရိုးရိုးလေး ပြောရရင်

include နဲ့ require က PHP ဖိုင်တစ်ခုက တခြားဖိုင်တစ်ခုကို ငှားသုံးခွင့် ပေးတာပါ။ Navigation menu တစ်ခုတည်းကို page သုံးဆယ်မှာ copy ကူးထည့်မယ့်အစား menu.php ထဲမှာ တစ်ကြိမ်ရေးပြီး page တစ်ခုချင်းစီမှာ 'ဒီဖိုင်ကို ဒီနေရာမှာ ထည့်ပေး' ဆိုတဲ့ စာကြောင်းတစ်ကြောင်းပဲ ရေးလိုက်ပါတယ်။ Menu ကို ပြင်လိုက်ရင် page အားလုံး အတူတူ ပြောင်းသွားပါတယ်။

ဒါက 'ကတ်နံပါတ် ၇ ထဲက အတိုင်း ဆော့စ်ပြင်ပါ' လို့ ရေးထားတဲ့ ဟင်းချက်နည်းနဲ့ တူပါတယ်၊ ကတ်ကို ဟင်းချက်နည်းထဲ copy ကူးမထည့်ဘဲ ညွှန်ပြလိုက်ရုံပါပဲ။ PHP က အဲဒီစာကြောင်းကို ရောက်တဲ့အခါ ညွှန်းထားတဲ့ဖိုင်ကို ဖတ်ပြီး ဒီနေရာမှာတင် run ပေးပါတယ်။

Problem → Why → Solution

ပြဿနာ: copy ကူးထားတဲ့ code နဲ့ ပျောက်သွားတဲ့ ဖိုင်

Page သုံးဆယ်စလုံးမှာ header ကို copy ကူးထားတယ် ဆိုပါစို့။ Logo ပြောင်းတဲ့အခါ တစ်ယောက်က ၂၇ ခုကို ပြင်ပြီး ကျန်သုံးခုကို မေ့သွားပါတယ်။ အဲဒါကို ရှောင်ဖို့ include ကို စသုံးကြတဲ့အခါ နောက်ပြဿနာတွေ ပေါ်လာပါတယ်: helper function တွေပါတဲ့ ဖိုင်ကို page တစ်ခုက နှစ်ကြိမ် load မိတာနဲ့ Cannot redeclare money() ဆိုတဲ့ fatal error ဖြစ်ပါတယ်၊ config ဖိုင်ကို include နဲ့ ခေါ်ထားပြီး path မှားနေရင် warning တစ်ကြောင်းပဲ ပေါ်ပြီး ကျန်တဲ့ page က database setting မပါဘဲ ဆက် run သွားပါတယ်။

ဖိုင်တွေကို မျှဝေသုံးစွဲဖို့ဆိုရင် စည်းကမ်းတွေ လိုပါတယ်: ဖိုင်ကို ဘယ်လိုရှာမလဲ၊ ဖိုင်မရှိရင် ဘာလုပ်မလဲ၊ နှစ်ခါ load မဖြစ်အောင် ဘယ်လိုကာကွယ်မလဲ ဆိုတာတွေပါ။

page-a.php
<?php
include 'config.php';      // wrong path? only a warning, the page keeps going
include 'helpers.php';
include 'helpers.php';     // pasted from another page: declares money() twice

echo money($price);        // Fatal error: Cannot redeclare money()

Statement လေးမျိုး ရှိရတဲ့ အကြောင်း

ဖိုင်တစ်ခုချင်းစီရဲ့ အရေးပါမှုနဲ့ သဘောသဘာဝက မတူပါဘူး။ Database config မပါရင် page အလုပ်မလုပ်နိုင်လို့ အဲဒီဖိုင်ပျောက်ရင် ကျယ်ကျယ်လောင်လောင် အစောကြီး ရပ်သင့်ပါတယ်၊ အလှဆင်တဲ့ banner ကတော့ ပျောက်ရင် ဆက်သွားလို့ ရပါတယ်။ Function ပါတဲ့ ဖိုင်ကို နှစ်ခါ load လို့ မရတဲ့အတွက် 'တစ်ခါပဲ' ဆိုတဲ့ စည်းကမ်းလည်း လိုပါတယ်။ include, require နဲ့ _once ပုံစံတွေက ဒီစည်းကမ်းတွေကို ကိုယ်စားပြုတာမို့ ဖိုင်ရဲ့ သဘောနဲ့ ကိုက်တဲ့ statement ကို ရွေးရပါတယ်။

Core concept: ဖိုင်ကို ဒီနေရာမှာ run ပေးပါ

include နဲ့ require က တခြား PHP ဖိုင်ကို ဖတ်ပြီး statement ရှိတဲ့ နေရာမှာတင် code ကို ကူးထည့်ထားသလို run ပေးပါတယ်။ Header, footer, config array, helper function စုတွေကို copy ကူးမနေဘဲ စာမျက်နှာအများကြီးက မျှသုံးနိုင်တာ ဒီနည်းနဲ့ပါ။ အဲဒီဖိုင်ထဲမှာ PHP tag အပြင်ဘက်က HTML တွေလည်း ဒီနေရာမှာပဲ output အဖြစ် ထွက်သွားပါတယ်။

Statement လေးမျိုး ရှိပါတယ်။ include နဲ့ require ရဲ့ ကွာခြားချက်က ဖိုင်ကို ရှာမတွေ့ ဒါမှမဟုတ် ဖတ်မရတဲ့အခါ အပြုအမူပါ: include က warning ပေးပြီး false ပြန်ကာ ဆက် run ပါတယ်၊ require ကတော့ warning ပေးပြီးနောက် Error ကို ပစ်ပါတယ်၊ ဖမ်းမထားရင် script ကို ရပ်ပစ်ပါတယ်။ _once ပုံစံတွေက load ပြီးသား ဖိုင်တွေကို (resolve လုပ်ပြီးသား path နဲ့) မှတ်ထားပြီး ထပ်ခေါ်ရင် ကျော်ကာ true ပြန်ပါတယ်။ Function ဒါမှမဟုတ် class ကြေညာတဲ့ ဖိုင်တွေအတွက် သုံးပါ၊ ဘာလို့လဲဆိုတော့ function တစ်ခုတည်းကို နှစ်ကြိမ် ကြေညာရင် fatal error ဖြစ်လို့ပါ။

ဖိုင်ရဲ့ အရေးပါမှုအလိုက် ရွေးပါ: ဖိုင်မပါရင် page အလုပ်မလုပ်နိုင်ဘူး (database config, autoloader, function စု) ဆိုရင် require_once ဒါမှမဟုတ် require ကို သုံးပါ၊ failure က ကျယ်ကျယ်လောင်လောင်နဲ့ စောစောပေါ်သင့်ပါတယ်။ ရှိရင်ရှိ မရှိရင်လည်းရတဲ့ အလှဆင်ဖိုင်ဆိုရင် include ကို သုံးလို့ရပါတယ်၊ ဒါပေမဲ့ ရလဒ်ကို အားကိုးရင် return value ကို စစ်ပါ။

Included code က include လုပ်လိုက်တဲ့ စာကြောင်းရဲ့ variable scope ထဲမှာ run ပါတယ်။ Script ရဲ့ အပေါ်ဆုံးကနေ include ရင် အဲဒီဖိုင်ရဲ့ variable တွေက global ဖြစ်ပြီး function ထဲကနေ include ရင်တော့ အဲဒီ function ထဲမှာပဲ local ဖြစ်ပါတယ်။ ဖိုင်တစ်ခုက return ကိုလည်း သုံးနိုင်ပြီး include/require က အဲဒီ value ကို ပြန်ပေးပါတယ်၊ array ကို return လုပ်တဲ့ config ဖိုင်က global variable တွေ သတ်မှတ်တဲ့ ဖိုင်ထက် အများကြီး သပ်ရပ်ပါတယ်။

နောက်ဆုံးအချက်က path တွေကို ဖိုင်ရဲ့ ကိုယ်ပိုင်တည်နေရာကနေ __DIR__ နဲ့ တည်ဆောက်ပါ။ 'config.php' လို့ ရေးရင် include_path နဲ့ တခြားနေရာတွေမှာ ရှာပေးတာ ဖြစ်ပြီး ./ နဲ့ စတဲ့ path က script ကို launch လုပ်တဲ့ directory ပေါ် မူတည်ပါတယ်၊ laptop ပေါ်မှာ အလုပ်လုပ်ပေမဲ့ cron မှာ ပျက်ပါတယ်။

Include ကို 'ဒီနေရာမှာ ဖိုင်ကို ထည့်' လို့ မြင်ပါ
  1. Statement က သူ့စာကြောင်းမှာ run
  2. PHP က path နဲ့ ဖိုင်ကို ရှာ
  3. ဖိုင်က ခေါ်တဲ့ scope ထဲမှာ run
  4. Output ဒါမှမဟုတ် return value ပြန်လာ

ဖိုင်ရှာမတွေ့ရင် include က false ပေးပြီး ဆက်သွားပါတယ်၊ require ကတော့ Error ပစ်ပါတယ်။

အထွေထွေ ပုံစံ

php
require_once __DIR__ . '/path/to/file.php';          // must exist, load once
require __DIR__ . '/path/to/file.php';               // must exist, load every time
include __DIR__ . '/path/to/partial.php';            // optional, load every time
include_once __DIR__ . '/path/to/optional.php';      // optional, load once

$value = require __DIR__ . '/path/to/config.php';    // file ends with: return ...;

ဖိုင်မပါရင် မရတာဆိုရင် require ပုံစံကို သုံးပါ၊ function ဒါမှမဟုတ် class ကြေညာတဲ့ ဖိုင်တွေအတွက် _once ကို ရွေးပါ၊ path ရဲ့ အစကို __DIR__ ကနေ စပါ။

Worked example: config, helper နဲ့ template ကို ဖိုင်သုံးခုနဲ့ ခွဲခြင်း

Command line ပေါ်မှာ ဖိုင်အစုအဝေး မရှိလို့ ဒီ sample က temp directory ထဲမှာ ဖိုင်သုံးခု (config.php, helpers.php, header.php) ကို ကိုယ်တိုင် ဖန်တီးပြီး include နဲ့ require တွေကို သုံးပြပါတယ်။ ဖိုင်တွေကို နောက်ဆုံးမှာ ပြန်ဖျက်ပါတယ်၊ ဒါကြောင့် ဘာမှ ကျန်မနေပါဘူး။ Output မှာ absolute path တစ်ခုမှ မပါအောင်လည်း ရေးထားပါတယ်။

include-demo.php
<?php
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'inc_' . bin2hex(random_bytes(4));
mkdir($dir);

file_put_contents($dir . '/config.php', <<<'FILE'
<?php
return ['site' => 'Tea Shop', 'currency' => 'MMK'];
FILE);

file_put_contents($dir . '/helpers.php', <<<'FILE'
<?php
function money(int $amount): string
{
    return number_format($amount) . ' MMK';
}
FILE);

file_put_contents($dir . '/header.php', <<<'FILE'
<h1><?= htmlspecialchars($title) ?></h1>
FILE);

$config = require $dir . '/config.php';
echo 'site: ', $config['site'], "\n";

require_once $dir . '/helpers.php';
$again = require_once $dir . '/helpers.php';
echo 'second require_once returned: ', var_export($again, true), "\n";
echo money(2500), "\n";

$title = 'Welcome';
include $dir . '/header.php';
echo "\n";

$warnings = [];
set_error_handler(function (int $level, string $message) use (&$warnings): bool {
    if (preg_match('/Failed to open stream: (.+)$/', $message, $match)) {
        $warnings[] = $match[1];
    }
    return true;
});
$result = include $dir . '/missing.php';
restore_error_handler();
echo 'missing file, include returned: ', var_export($result, true), "\n";
echo 'warning text: ', $warnings[0] ?? '(none)', "\n";

array_map('unlink', glob($dir . '/*.php'));
rmdir($dir);

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. A config file that returns$config = require $dir . '/config.php';

    config.php က return ['site' => ...] နဲ့ ပြီးတဲ့အတွက် require က array ကို ပြန်ပေးပါတယ်။ Global variable တစ်ခုမှ မပေါ်ဘဲ ရလဒ်ကို ကိုယ်လိုချင်တဲ့ နာမည်နဲ့ ယူလို့ ရပါတယ်။

  2. Load a function file once$again = require_once $dir . '/helpers.php';

    ဒုတိယအကြိမ် require_once ခေါ်ရင် ဖိုင်ကို ထပ်မ run ပါဘူး၊ ဒါကြောင့် money() ကို ထပ်ကြေညာတဲ့ fatal error မဖြစ်ပါဘူး။ ပြန်ပေးတဲ့ value က true ပါ။

  3. The included file sees your variable$title = 'Welcome';

    header.php က $title ကို သူ့ဘာသာ သတ်မှတ်ထားတာ မဟုတ်ပါဘူး၊ include လုပ်တဲ့ စာကြောင်းရဲ့ scope ထဲက variable ကို တိုက်ရိုက်သုံးပါတယ်။ ဒါကြောင့် include မခေါ်ခင် $title ကို ပေးထားရပါတယ်။

  4. Catch the warning textif (preg_match('/Failed to open stream: (.+)$/', $message, $match)) {

    include က ဖိုင်မရှိရင် warning ပေးပါတယ်၊ warning စာသားထဲမှာ path အပြည့်အစုံ ပါတတ်လို့ error handler နဲ့ ဖမ်းပြီး 'No such file or directory' ဆိုတဲ့ အပိုင်းကိုပဲ ထုတ်ယူထားပါတယ်။

  5. Inspect the return value$result = include $dir . '/missing.php';

    ဖိုင်မရှိတဲ့အခါ include က false ပြန်ပေးပြီး script ဆက် run ပါတယ်။ Optional ဖိုင်တွေအတွက် ဒီ false ကို စစ်ပြီး ဆုံးဖြတ်လို့ ရပါတယ်။

You should see
site: Tea Shop
second require_once returned: true
2,500 MMK
<h1>Welcome</h1>
missing file, include returned: false
warning text: No such file or directory

include နဲ့ require ဖိုင်ပျောက်တဲ့အခါ

Statementဖိုင်ပျောက်ရင်ယခင်က load ပြီးသားဆိုရင်
includeWarning၊ false ပြန်၊ ဆက် runထပ် load လုပ်
requireWarning ပြီး Error ပစ်၊ ဖမ်းမထားရင် ရပ်ထပ် load လုပ်
include_onceWarning၊ false ပြန်၊ ဆက် runကျော်ပြီး true ပြန်
require_oncerequire လိုပဲကျော်ပြီး true ပြန်

ဒါကို ကိုယ်တိုင် ကြည့်ရအောင်။ အောက်က script က မရှိတဲ့ဖိုင်ကို include နဲ့ require နှစ်မျိုးလုံးနဲ့ ခေါ်ပါတယ်။ Warning စာသားမှာ path ပါတာကြောင့် error handler နဲ့ ဖိုင်ထဲ မဖော်ပြဘဲ ဖုံးထားပါတယ်။ PHP 8 မှာ require ရဲ့ ရလဒ်ဟာ Error exception ဖြစ်လို့ try/catch နဲ့ ဖမ်းလို့ ရပါတယ်။ ဖမ်းမထားရင်တော့ 'Fatal error: Uncaught Error: Failed opening required ...' ဆိုပြီး script ရပ်သွားပါတယ်။

missing-file.php
<?php
$name = 'nope_' . bin2hex(random_bytes(4)) . '.php';
$missing = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $name;

set_error_handler(function (int $level): bool {
    return true;   // swallow the warning so the output stays tidy
});

$value = include $missing;
echo 'include -> returned ', var_export($value, true), ', script continues', "\n";

try {
    require $missing;
} catch (Error $e) {
    echo 'require -> threw ', get_class($e), ', an uncaught one would stop here', "\n";
}
restore_error_handler();
text
include -> returned false, script continues
require -> threw Error, an uncaught one would stop here

Included file က ခေါ်တဲ့ scope ထဲမှာ run တယ်

ဒီအချက်က template တွေရဲ့ အခြေခံပါ။ အောက်ကဥပမာမှာ greeting.php က $name ကို သူ့ဘာသာ မကြေညာထားပါဘူး၊ render() function ထဲက $name ကို တိုက်ရိုက်ဖတ်ပါတယ်။ include ကို function ထဲမှာ ခေါ်ထားလို့ ဖိုင်က သတ်မှတ်တဲ့ $message က render() ရဲ့ local variable ဖြစ်ပြီး global scope ကို မယိုစိမ့်ပါဘူး။ ဒါကြောင့် template တွေကို function ထဲက ခေါ်ရင် variable တွေ ဘယ်မှာပေါ်လဲ ထိန်းလို့ရပါတယ်။

include-scope.php
<?php
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'scope_' . bin2hex(random_bytes(4));
mkdir($dir);
file_put_contents($dir . '/greeting.php', <<<'FILE'
<?php
$message = "Hello, $name!";
FILE);

function render(string $dir, string $name): string
{
    include $dir . '/greeting.php';   // runs inside render()'s scope
    return $message;
}

echo render($dir, 'Ada'), "\n";
echo 'leaked into the global scope: ', var_export(isset($message), true), "\n";

unlink($dir . '/greeting.php');
rmdir($dir);
text
Hello, Ada!
leaked into the global scope: false

Path မှာ __DIR__ ကို ဘာကြောင့် သုံးရလဲ

'config/app.php' လို path အလွတ်ကို PHP က include_path ထဲက entry တစ်ခုချင်းစီမှာ ရှာပြီး ပြီးမှ ခေါ်တဲ့ script ရဲ့ ကိုယ်ပိုင် directory၊ နောက်ဆုံး current working directory မှာ ရှာပါတယ်။ ./ ဒါမှမဟုတ် ../ နဲ့ စတဲ့ path ကိုတော့ current working directory နဲ့ပဲ ယှဉ်ပါတယ်၊ Command line ကနေ run ရင် အဲဒါက php ကို launch လုပ်ခဲ့တဲ့ folder ဖြစ်လို့ ဖိုင်ရဲ့ ကိုယ်ပိုင် folder နဲ့ မတူနိုင်ပါဘူး။ __DIR__ ကတော့ ဒီစာကြောင်းရှိတဲ့ ဖိုင်ရဲ့ folder ကို အမြဲ ပေးတာမို့ ဘယ်ကနေ run run အလုပ်လုပ်ပါတယ်။

paths.php
<?php
// Fragile: depends on include_path and on where the script was launched from
require 'config/app.php';
require './config/app.php';

// Reliable: always relative to THIS file
require __DIR__ . '/config/app.php';
require dirname(__DIR__) . '/vendor/autoload.php';

Visitor ကို path မရွေးခိုင်းပါနဲ့

include $_GET['page'] . '.php'; လို code က လွယ်ကူပေမဲ့ တိုက်ခိုက်လိုသူတွေအတွက် တံခါးဖွင့်ပေးထားတာပါပဲ။ ../../ တွေပါတဲ့ path ကို ပို့ရင် ကိုယ်မရည်ရွယ်တဲ့ PHP ဖိုင်တွေကို run နိုင်ပြီး၊ ဖိုင်ကို ဘယ်လိုပဲ ထည့်နိုင်တဲ့ တခြား အားနည်းချက်နဲ့ ပေါင်းရင် ပိုဆိုးပါတယ်။ ဒါကို Local File Inclusion လို့ ခေါ်ပါတယ်။ remote URL ကို include လုပ်တာ (allow_url_include) ကတော့ default အနေနဲ့ ပိတ်ထားပေမဲ့ local ဖိုင်တွေကတော့ အန္တရာယ်ရှိနေဆဲပါ။

ပြင်နည်းက visitor ပို့တဲ့ စာသားကို path အဖြစ် မသုံးဘဲ ခွင့်ပြုထားတဲ့ စာရင်း (allow-list) အတွက် key တစ်ခုအဖြစ်ပဲ သုံးတာပါ။ Key က စာရင်းထဲ ရှိမှ ကိုယ်တိုင်ရေးထားတဲ့ ဖိုင်နာမည်ကို သုံးပါတယ်။

page-allow-list.php
<?php
$pages = [
    'home' => 'home.php',
    'about' => 'about.php',
    'contact' => 'contact.php',
];

foreach (['about', '../../etc/passwd', 'HOME', 'contact'] as $requested) {
    if (array_key_exists($requested, $pages)) {
        echo str_pad($requested, 18), 'include ', $pages[$requested], "\n";
    } else {
        echo str_pad($requested, 18), 'rejected, falling back to home.php', "\n";
    }
}
text
about             include about.php
../../etc/passwd  rejected, falling back to home.php
HOME              rejected, falling back to home.php
contact           include contact.php

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ include နဲ့ require

Website အသေးတွေမှာ layout ကို partial ဖိုင်တွေ (header.php, footer.php, menu.php) အဖြစ် ခွဲပြီး page တိုင်းက include လုပ်ပါတယ်။ Application တိုင်းရဲ့ front controller (index.php) ရဲ့ အပေါ်ဆုံးမှာ require __DIR__ . '/../vendor/autoload.php'; နဲ့ config ဖိုင်ကို require လုပ်တာကိုလည်း တွေ့ရပါလိမ့်မယ်။ WordPress theme တွေက get_header() လို function တွေနဲ့ ဒီအယူအဆကိုပဲ ထုပ်ပိုးထားပါတယ်။

Project ကြီးလာတဲ့အခါ class တွေကို ကိုယ်တိုင် require_once လုပ်မနေတော့ဘဲ autoloader က ကိုယ့်ကိုယ်စား ဖိုင်တွေကို load ပေးပါတယ် (နောက် lesson မှာ လေ့လာပါမယ်)။ ဒါပေမဲ့ bootstrap ဖိုင်၊ config ဖိုင်၊ template တွေအတွက် include နဲ့ require ကို ဆက်သုံးနေရဆဲ ဖြစ်လို့ ဒီစည်းကမ်းတွေကို သိထားဖို့ လိုပါတယ်။

  • Layout partial: header.php, footer.php, menu.php ကို page တိုင်းက include လုပ်ပြီး ပြင်စရာရှိရင် တစ်နေရာတည်းမှာ ပြင်ပါတယ်။
  • Bootstrap နဲ့ config: front controller က require နဲ့ autoloader နဲ့ config array ကို load ပြီး မရှိရင် ချက်ချင်း ရပ်ပါတယ်။
  • Page routing: ?page= တန်ဖိုးကို allow-list နဲ့ တိုက်ပြီး ကိုက်တဲ့ template ကိုပဲ include လုပ်ပါတယ်။

အမှားများ

include နဲ့ require မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

မရှိမဖြစ်ဖိုင်ကို include နဲ့ ခေါ်ခြင်း

config.php ကို include နဲ့ ခေါ်ပြီး path မှားရင် warning တစ်ကြောင်းပဲ ပေါ်ပြီး page က database setting မပါဘဲ ဆက်သွားကာ နောက်မှ ရှုပ်ထွေးတဲ့ error တွေ ထွက်လာပါတယ်။ မရှိမဖြစ်ဖိုင်ကို require နဲ့ ခေါ်ပါ။

Function ဖိုင်ကို _once မပါဘဲ ခေါ်ခြင်း

helpers.php ကို ဖိုင်နှစ်ခုက include ရင် Cannot redeclare money() ဆိုတဲ့ fatal error ပေါ်ပါတယ်။ Function ဒါမှမဟုတ် class ကြေညာတဲ့ ဖိုင်တွေအတွက် require_once ကို သုံးပါ။

Working directory ပေါ် မှီခိုတဲ့ relative path

require './config/app.php'; က browser နဲ့ ဖွင့်ရင် အလုပ်လုပ်ပေမဲ့ cron ဒါမှမဟုတ် command line ကနေ တခြား folder က run ရင် ပျက်ပါတယ်။ __DIR__ နဲ့ ဖိုင်ကို ညွှန်ပါ။

Visitor ပို့တဲ့ စာသားကို path အဖြစ် သုံးခြင်း

include $_GET['page'] . '.php'; က ကိုယ်မရည်ရွယ်တဲ့ ဖိုင်တွေကို run စေနိုင်ပါတယ်။ str_replace('..', '', ...) လို ဖြတ်တောက်နည်းတွေကို အားမကိုးပါနဲ့၊ allow-list ကိုသာ သုံးပါ။

ဖိုင်အပြင်ဘက်က space ကြောင့် headers already sent

Included ဖိုင်ရဲ့ <?php အရှေ့မှာ blank line (ဒါမှမဟုတ် ?> နောက်က newline) တစ်ကြောင်းပါရင် output အဖြစ် ထွက်သွားပြီး နောက်က header() ဒါမှမဟုတ် session_start() က 'Cannot modify header information - headers already sent' warning ပေးပါတယ်။ PHP သက်သက်ပါတဲ့ ဖိုင်တွေမှာ ?> ကို ထည့်မထားပါနဲ့။

Best Practices

Best practices

  • မရှိမဖြစ်ဖိုင်တွေအတွက် require ပုံစံ၊ ရှိရင်ရှိ မရှိရင်လည်းရတဲ့ partial တွေအတွက် include ပုံစံကို သုံးပါ။
  • Function ဒါမှမဟုတ် class ကြေညာတဲ့ ဖိုင်တိုင်းကို _once နဲ့ ခေါ်ပါ၊ ဒါမှမဟုတ် autoloader ကို သုံးပါ။
  • Path တိုင်းကို __DIR__ ကနေ စပါ၊ working directory ကို မယုံပါနဲ့။
  • Config ဖိုင်က return နဲ့ array တစ်ခု ပေးပါ၊ global variable တွေ မသတ်မှတ်ပါနဲ့။

Mini challenge

Mini challenge: လုံခြုံတဲ့ config loader

loadConfig(string $dir, string $name): array ကို ရေးပါ။ $name ကို allow-list နဲ့ စစ်ပြီး ($dir ထဲက) ဖိုင်ကို require လုပ်ကာ အဲဒီဖိုင် return ပေးတဲ့ array ကို ပြန်ပေးရပါမယ်။ မှားယွင်းတဲ့ အခြေအနေတိုင်းအတွက် ရှင်းလင်းတဲ့ exception ကို ပစ်ပါ။

  • $name က a-z နဲ့ underscore ပဲ ပါရမယ် (မဟုတ်ရင် InvalidArgumentException)
  • ဖိုင်မရှိရင် RuntimeException
  • ဖိုင်က array မဟုတ်တာ return လုပ်ရင် UnexpectedValueException
  • Temp directory ထဲမှာ app.php (array), bad.php (string) ကို ဖန်တီးပြီး app, bad, missing, ../app ကို စမ်းကာ ရလဒ်တွေကို print ထုတ်ပါ၊ အဆုံးမှာ ဖိုင်တွေကို ဖျက်ပါ
config-loader.php
<?php
function loadConfig(string $dir, string $name): array
{
    // TODO: reject names that are not lowercase letters and underscores
    // TODO: build the path from $dir and $name (never from anything else)
    // TODO: throw RuntimeException when the file does not exist
    // TODO: require the file and check that it returned an array
}

$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cfg_' . bin2hex(random_bytes(4));
mkdir($dir);

// TODO: write app.php (returns ['name' => 'Tea Shop', 'debug' => true]) and bad.php
// TODO: try loadConfig for 'app', 'bad', 'missing' and '../app'; print each outcome
// TODO: delete the files and the directory

ရလဒ်: app က Tea Shop ကို debug on နဲ့ ပြပြီး bad က UnexpectedValueException၊ missing က RuntimeException၊ ../app က InvalidArgumentException ကို message နဲ့တကွ ပြပါမယ်။

အရိပ်အမြွက် ကြည့်ရန်

preg_match('/^[a-z_]+$/D', $name) ကို အရင်စစ်ပါ၊ ပြီးမှ is_file() နဲ့ require ကို သုံးပါ။ Test အတွက် file_put_contents နဲ့ '<?php return ...;' စာကြောင်းတွေကို ရေးပါ။

အဖြေ ကြည့်ရန်
config-loader.php
<?php
function loadConfig(string $dir, string $name): array
{
    if (preg_match('/^[a-z_]+$/D', $name) !== 1) {
        throw new InvalidArgumentException('invalid config name');
    }

    $path = $dir . DIRECTORY_SEPARATOR . $name . '.php';
    if (!is_file($path)) {
        throw new RuntimeException("config '$name' not found");
    }

    $config = require $path;
    if (!is_array($config)) {
        throw new UnexpectedValueException("$name.php must return an array");
    }

    return $config;
}

$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cfg_' . bin2hex(random_bytes(4));
mkdir($dir);
$app = "<?php\nreturn ['name' => 'Tea Shop', 'debug' => true];\n";
file_put_contents($dir . '/app.php', $app);
file_put_contents($dir . '/bad.php', "<?php\nreturn 'oops';\n");

foreach (['app', 'bad', 'missing', '../app'] as $name) {
    try {
        $config = loadConfig($dir, $name);
        $mode = $config['debug'] ? 'on' : 'off';
        echo "$name: {$config['name']} (debug $mode)\n";
    } catch (Exception $e) {
        echo "$name: ", get_class($e), ': ', $e->getMessage(), "\n";
    }
}

array_map('unlink', glob($dir . '/*.php'));
rmdir($dir);

Quick Quiz 1 / 3

PHP 8 မှာ ဖိုင်မရှိရင် $r = include 'missing.php'; က ဘာဖြစ်မလဲ?

Quick Quiz 2 / 3

functions.php က money() ကို ကြေညာထားပြီး ဖိုင်နှစ်ခုက ဒီဖိုင်ကို load ပါတယ်။ Cannot redeclare money() error ကို ဘယ် statement က ရှောင်ပေးမလဲ?

Quick Quiz 3 / 3

?page=about လို query ကနေ page ရွေးခိုင်းမယ့်အခါ ဘယ်ဟာက အလုံခြုံဆုံးလဲ?

မှတ်ထားရမယ့် အချက်များ

  • include က ဖိုင်ပျောက်ရင် warning ပေးပြီး false ပြန်၊ require က Error ပစ်ပါတယ်၊ ဖိုင်ရဲ့ အရေးပါမှုအလိုက် ရွေးပါ။
  • _once ပုံစံတွေက ထပ်ခေါ်ရင် ကျော်ပြီး true ပြန်တာမို့ function ဒါမှမဟုတ် class ဖိုင်တွေအတွက် သင့်တော်ပါတယ်။
  • Included code က ခေါ်တဲ့ scope ထဲမှာ run ပြီး return နဲ့ value ပြန်ပေးနိုင်လို့ config ဖိုင်က array return လုပ်တာ သပ်ရပ်ပါတယ်။
  • Path ကို __DIR__ ကနေ စပါ၊ visitor ပို့တဲ့ စာသားကို path အဖြစ် မသုံးဘဲ allow-list နဲ့ပဲ ရွေးပါ။

PHP Language Reference — PHP Documentation Group

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Include & Require | Thuta Learning