ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Type declaration ဆိုတာ function ကို "ဒီနေရာမှာ integer လိုတယ်"၊ "ဒါက string ပြန်ပေးမယ်" လို့ ကြိုပြောထားတာပါ။ PHP က ပုံမှန်အားဖြင့် စိတ်ရှည်ပါတယ် — integer တောင်းထားတဲ့နေရာမှာ "5" ဆိုတဲ့ string ရောက်လာရင် 5 ဖြစ်အောင် တိတ်တိတ်လေး ပြောင်းပေးလိုက်ပါတယ်။ ဒါက အဆင်ပြေသလိုပဲ ဖြစ်ပေမယ့် မှားနေတာကိုလည်း ဖုံးကွယ်ပေးတတ်ပါတယ်။
ဖိုင်ရဲ့ အပေါ်ဆုံးမှာ declare(strict_types=1); ရေးလိုက်ရင် ဒီဖိုင်ထဲက function call တွေကို ပိုတင်းကျပ်စေပါတယ်။ အဲဒီအခါ integer တောင်းထားတဲ့ function ကို "5" ပို့ရင် PHP က ပြောင်းမပေးတော့ဘဲ TypeError ပစ်ပါတယ်။ ဒီ lesson က ဘယ်အချိန် ဘယ်ဖိုင်က ဘာကို ထိန်းချုပ်လဲဆိုတာ အသေးစိတ် ကြည့်ပါမယ်။
Problem → Why → Solution
ပြဿနာ: မမျှော်လင့်တဲ့ value တွေကို PHP က တိတ်တိတ် လက်ခံတယ်
Shopping cart function က int $qty တောင်းထားပါတယ်။ ဒါပေမယ့် Strict မဟုတ်တဲ့ file မှာ addToCart(true) ကို ခေါ်ရင် 1 ဖြစ်သွားပါတယ်၊ addToCart(2.9) ကို ခေါ်ရင် 2 ဖြစ်ပြီး (အပိုင်းကိန်း ပျောက်သွားပါတယ်)၊ addToCart("1e2") ကတော့ 100 ခု ထည့်ပါတယ်။ Error တစ်ခုမှ မထွက်ပါဘူး (2.9 အတွက်တော့ Deprecated: Implicit conversion from float 2.9 to int loses precision ပဲ ပေါ်ပါတယ်)။
Form ကနေ ရောက်လာတဲ့ ဒေတာက အမြဲ string ဖြစ်လို့ "3" လို value တွေကို PHP က ပြောင်းလိုက်တာက အကျိုးရှိတာ မှန်ပါတယ်။ ဒါပေမယ့် true၊ 2.9၊ "1e2" လို တခြား ဖြစ်နိုင်ခြေတွေကိုပါ လက်ခံလိုက်ရင် code ရဲ့ ကတိက ဝါးတားတားဖြစ်ပြီး bug တွေက နောက်မှ ပေါ်လာပါတယ်။
<?php
// No declare(strict_types=1): PHP converts whatever it can
function addToCart(int $qty): string
{
return "added $qty";
}
echo addToCart(true), "\n"; // added 1
echo addToCart(2.9), "\n"; // added 2 (Deprecated notice, fraction lost)
echo addToCart("1e2"), "\n"; // added 100ဘာကြောင့် ဒီလို လက်ခံတာလဲ
PHP ကို မူလကတည်းက type အလွန်ပြောင်းလွယ်တဲ့ ဘာသာစကားအဖြစ် တည်ဆောက်ထားလို့ default mode (coercive / weak mode) က ပြောင်းလို့ရတဲ့ scalar တွေကို ပြောင်းပေးပါတယ်။ ဒီအပြုအမူက အရင်ရှိပြီးသား code အဟောင်းတွေ မပျက်အောင် ထိန်းထားတာပါ။ Strict mode ကို ဖိုင်တစ်ခုချင်းစီမှာ ရွေးလို့ ရအောင် ထည့်ပေးထားလို့ code အသစ်ကို တင်းကျပ်စွာ ရေးပြီး အဟောင်းကို မထိဘဲ ထားနိုင်ပါတယ်။
Core concept: strict mode က "ခေါ်တဲ့ဖိုင်" ရဲ့ setting ကို လိုက်တယ်
PHP မှာ scalar type လေးမျိုး (int, float, string, bool) ရှိပြီး တခြား type declaration တွေကတော့ array, object, callable, ?int လို nullable၊ int|string လို union (PHP 8.0 ကစပြီး)၊ A&B လို intersection (PHP 8.1 ကစပြီး) တွေပါ။ Class, array, callable လို scalar မဟုတ်တဲ့ type တွေက အမြဲ တင်းကျပ်ပါတယ်၊ strict_types က scalar type တွေကိုပဲ ထိခိုက်ပါတယ်။ Default mode မှာ PHP က ပြောင်းလို့ရတဲ့ scalar တွေကို ပြောင်းပေးပြီး မရတဲ့ဟာတွေအတွက်ပဲ TypeError ပစ်ပါတယ်။ TypeError ဆိုတာ ဖမ်းလို့ရတဲ့ exception တစ်မျိုးဖြစ်ပြီး message ထဲမှာ function နာမည်၊ ဘယ် argument လဲ၊ ဘာ type လိုပြီး ဘာ type ရောက်လာလဲ ပါပါတယ်။
declare(strict_types=1); ကို ဖိုင်ရဲ့ ပထမဆုံး statement အဖြစ် ထည့်ရင် အဲဒီဖိုင်ထဲက call တွေမှာ exact type ကိုပဲ လက်ခံတော့ပါတယ်။ ချွင်းချက်တစ်ခုတည်းက int ကို float parameter ဆီ ပို့လို့ရတာပါ (int က float ဖြစ်အောင် ကျယ်ပြန့်သွားတာမို့ အချက်အလက် မဆုံးရှုံးလို့ပါ)။ "5" ကို int အတွက်၊ 5 ကို string အတွက်၊ 5.0 ကို int အတွက် ပို့ရင် TypeError ပါ။ Function ရဲ့ return value ကိုလည်း function ကို ကြေညာထားတဲ့ ဖိုင်ရဲ့ setting နဲ့ စစ်ပါတယ်။
အရေးကြီးဆုံး အချက်က strict_types က caller-sensitive ဖြစ်တာပါ။ Argument ကို စစ်တာကို function ကို ကြေညာထားတဲ့ ဖိုင်က မဆုံးဖြတ်ပါဘူး၊ call ကို ရေးထားတဲ့ ဖိုင်က ဆုံးဖြတ်ပါတယ်။ ဒါကြောင့် strict မဟုတ်တဲ့ ဖိုင်က strict library function ကို "21" ပို့ရင် ပြောင်းပေးပြီး အလုပ်လုပ်ပါတယ်။ ဒီအပြင် PHP ရဲ့ built-in function တွေ callback ခေါ်တာ (array_map က သင့် function ကို ခေါ်တာမျိုး) ကတော့ strict ဖိုင်ထဲက ရေးထားရင်တောင် weak mode နဲ့ပဲ အလုပ်လုပ်ပါတယ်။ နောက်ဆုံးအချက်အနေနဲ့ strict types က validation မဟုတ်ပါဘူး — int $age ကို -5 ပို့လည်း လက်ခံမှာဖြစ်လို့ ဘယ်လောက်ကနေ ဘယ်လောက်ထိ ဆိုတာတော့ ကိုယ်တိုင် စစ်ရပါမယ်။
- Call ကို ဖိုင်တစ်ခုထဲမှာ ရေးထား
- PHP က အဲဒီဖိုင်ရဲ့ strict flag ကို ဖတ်
- Strict: exact type၊ int→float ပဲ ကျယ်ပြန့်
- Weak: ပြောင်းလို့ရတာ ပြောင်းပေး
Return type ကတော့ function ကို ကြေညာထားတဲ့ ဖိုင်ရဲ့ setting နဲ့ စစ်တာမို့ argument နဲ့ return က သုံးမျိုးမတူပါဘူး။
အထွေထွေ ပုံစံ
<?php
declare(strict_types=1); // first statement; applies to this file only
function name(int $a, ?string $b, int|float $c): ReturnType
{
return $value; // checked against ReturnType
}
try {
name($x, $y, $z);
} catch (TypeError $e) {
// a wrong scalar type reached a parameter or a return
}declare ကို ဖိုင်ရဲ့ ပထမဆုံး statement မှာ ထည့်ရပါမယ် (အပေါ်မှာ comment ပဲ ရှိနိုင်တယ်)။ ဖိုင်တစ်ခုချင်းစီမှာ သီးခြား ရေးရပြီး include လုပ်တဲ့ တခြားဖိုင်တွေကို မသက်ရောက်ပါဘူး။ TypeError ကို try/catch နဲ့ ဖမ်းနိုင်ပါတယ်။
Example: strict file ထဲက total() function
ဒီ script က strict mode မှာ function တစ်ခုကို ခေါ်ပုံ လေးမျိုး ပြပါတယ် — ပုံမှန် call၊ int ကို float ဆီ ပို့တာ၊ string ပို့လို့ TypeError ဖြစ်တာ၊ နောက်ဆုံးတစ်ခုက type မှန်ပေမယ့် တန်ဖိုး မှားလို့ ကိုယ်တိုင် ရေးထားတဲ့ exception ပစ်တာပါ။ TypeError message ရဲ့ အဆုံးမှာ ", called in /path/file.php on line N" ပါလာတတ်လို့ strstr() နဲ့ အဲဒီအပိုင်းကို ဖြတ်ထားပါတယ်။
<?php
declare(strict_types=1);
function total(int $qty, float $price): float
{
if ($qty < 0 || $price < 0) {
throw new InvalidArgumentException('positive values required');
}
return $qty * $price;
}
echo total(3, 19.5), "\n";
echo total(2, 10), "\n";
try {
echo total("3", 19.5), "\n";
} catch (TypeError $e) {
echo "TypeError: ", strstr($e->getMessage(), ', called in', true), "\n";
}
try {
echo total(-1, 5), "\n";
} catch (InvalidArgumentException $e) {
echo "Invalid: ", $e->getMessage(), "\n";
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The declaration
declare(strict_types=1);ဒါက ဖိုင်ရဲ့ ပထမ statement ဖြစ်ရမယ်။ ဒီ line ရှိလို့ အောက်က call တွေအားလုံးမှာ scalar type ကို exact စစ်ပါတယ်။
- The signature
function total(int $qty, float $price): floatParameter နှစ်ခုနဲ့ return type ကို type ပေးထားပါတယ်။ strict mode မှာ $qty က int အစစ်၊ $price က float (ဒါမှမဟုတ် int) ဖြစ်ရပါမယ်။
- The guard
throw new InvalidArgumentException('positive values required');Type မှန်ပေမယ့် တန်ဖိုး မှားတာ (-1) ကို type ကစစ်မပေးနိုင်လို့ ကိုယ်တိုင် စစ်ပြီး exception ပစ်ထားပါတယ်။ Strict types က validation အစား မဟုတ်ပါဘူး။
- int widens to float
echo total(2, 10), "\n";10 က int ဖြစ်ပေမယ့် $price က float မို့ strict mode မှာတောင် လက်ခံပါတယ်။ ဒါက ခွင့်ပြုထားတဲ့ တစ်ခုတည်းသော ပြောင်းလဲမှုပါ၊ ရလဒ် 20 ပါ။
- Catching TypeError
echo "TypeError: ", strstr($e->getMessage(), ', called in', true), "\n";"3" က string မို့ TypeError ပစ်ပါတယ်။ strstr() ရဲ့ တတိယ argument true က ', called in' ရဲ့ အရင်အပိုင်းကိုပဲ ယူပေးလို့ file path မပါတဲ့ message ရပါတယ်။
58.5
20
TypeError: total(): Argument #1 ($qty) must be of type int, string given
Invalid: positive values requiredWeak mode က ဘာတွေ ပြောင်းပေးလဲ
strict_types မပါတဲ့ဖိုင်မှာ int parameter က ပြောင်းလို့ရတဲ့ value တွေအများကြီး လက်ခံပါတယ်။ အောက်က script က function တစ်ခုကို value ကွဲပြားစွာ ပို့ပြီး ဘာဖြစ်လဲ ပြပါတယ်။ Deprecated notice တွေကို set_error_handler() နဲ့ ဖမ်းပြီး ဘေးမှာ ပြထားပါတယ်၊ ဒါမှ output က စက်တစ်လုံးနဲ့တစ်လုံး မကွာပါဘူး။
<?php
$notes = [];
set_error_handler(function (int $level, string $message) use (&$notes): bool {
$notes[] = $message;
return true;
});
function describe(int $n): string
{
return "int($n)";
}
$inputs = ["5", "5.0", " 5", "1e2", true, 5.0, 5.5, "5 apples", "abc", null];
foreach ($inputs as $value) {
$notes = [];
try {
$result = describe($value);
} catch (TypeError $e) {
$result = "TypeError";
}
echo str_pad(var_export($value, true), 11), " => ", $result;
if ($notes) {
echo " [", $notes[0], "]";
}
echo "\n";
}ရလဒ်အရ "5"၊ "5.0"၊ " 5"၊ "1e2"၊ true နဲ့ 5.0 အားလုံး int ဖြစ်သွားပါတယ် (1e2 က 100၊ true က 1)။ 5.5 ကို 5 အဖြစ် ဖြတ်ပြီး Implicit conversion from float 5.5 to int loses precision ဆိုတဲ့ Deprecated notice တက်ပါတယ်၊ ဒါက ဒီ lesson ရဲ့ PHP 8.3 မှာ တွေ့ရတဲ့ အပြုအမူဖြစ်ပြီး deprecated ဖြစ်တဲ့ အပြုအမူက နောက် version တွေမှာ ပြောင်းနိုင်ပါတယ်။ "5 apples"၊ "abc" နဲ့ null ကတော့ TypeError ပါ၊ ဒါကြောင့် weak mode မှာတောင် ဘာမဆို မဝင်ပါဘူး။
Strict mode ထဲမှာ built-in function နဲ့ callback
strict file ထဲကနေ PHP ရဲ့ built-in function ကို ခေါ်တာတောင် strict ပါပဲ — strlen(123) က TypeError ပေးပါတယ်။ ဒါပေမယ့် array_map($double, ["1", "2"]) မှာ $double ကို ခေါ်သူက သင့် code မဟုတ်ဘဲ array_map ကိုယ်တိုင်ဖြစ်လို့ weak mode နဲ့ အလုပ်လုပ်ပြီး "1" က 1 ဖြစ်သွားပါတယ်။ Return type ကိုလည်း ပြန်ကြည့်ပါ — five(): int က 5.0 ပြန်ရင် float ကို int အဖြစ် မလက်ခံတော့လို့ TypeError ပါ။
<?php
declare(strict_types=1);
function attempt(string $label, callable $fn): void
{
try {
$result = $fn();
} catch (TypeError $e) {
$message = strstr($e->getMessage(), ', called in', true) ?: $e->getMessage();
$result = "TypeError: " . $message;
}
echo $label, ": ", $result, "\n";
}
$double = fn(int $n): int => $n * 2;
attempt("direct call", fn() => $double("21"));
attempt("array_map callback", fn() => implode(",", array_map($double, ["1", "2"])));
attempt("built-in strlen(123)", fn() => strlen(123));
function half(float $n): float { return $n / 2; }
attempt("int into float", fn() => half(5));
function five(): int { return 5.0; }
attempt("float returned as int", fn() => five());Output: direct call က TypeError၊ array_map callback က 2,4၊ built-in strlen(123) က TypeError၊ int into float က 2.5၊ float returned as int က TypeError ပါ။ Callback ထဲမှာ strict check လိုရင် callback ထဲမှာပဲ is_int() စတဲ့ စစ်ဆေးမှု ထည့်ပါ၊ ဒါမှမဟုတ် array_map မသုံးဘဲ foreach နဲ့ ကိုယ်တိုင် ခေါ်ပါ။
ဘယ်ဖိုင်က ခေါ်လဲ: ဖိုင်နှစ်ခု နမူနာ
Caller-sensitive ဆိုတာကို အတည်ပြုဖို့ အောက်က script က temp folder ထဲမှာ ဖိုင်လေးခု ဖန်တီးပြီး include လုပ်ပါတယ်။ strict_lib.php မှာ strict function နှစ်ခု၊ loose_lib.php မှာ strict မဟုတ်တဲ့ function တစ်ခု ရှိပါတယ်။ ပြီးတော့ strict မဟုတ်တဲ့ caller တစ်ခုနဲ့ strict caller တစ်ခုက တစ်ခုချင်းစီကို ခေါ်ပါတယ်။
<?php
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "strict_demo_" . getmypid();
mkdir($dir);
file_put_contents($dir . "/strict_lib.php", '<?php
declare(strict_types=1);
function twice(int $n): int { return $n * 2; }
function sloppy(): int { return "7"; }
');
file_put_contents($dir . "/loose_lib.php", '<?php
function triple(int $n): int { return $n * 3; }
');
file_put_contents($dir . "/loose_caller.php", '<?php
require __DIR__ . "/strict_lib.php";
echo twice("21"), "\n";
try {
echo sloppy(), "\n";
} catch (TypeError $e) {
echo "TypeError: ", $e->getMessage(), "\n";
}
');
file_put_contents($dir . "/strict_caller.php", '<?php
declare(strict_types=1);
require __DIR__ . "/loose_lib.php";
try { echo triple("5"), "\n"; } catch (TypeError $e) {
echo "TypeError: ", strstr($e->getMessage(), ", called in", true), "\n";
}
');
echo "non-strict caller, strict library:\n";
include $dir . "/loose_caller.php";
echo "strict caller, non-strict library:\n";
include $dir . "/strict_caller.php";
array_map("unlink", glob($dir . "/*.php"));
rmdir($dir);ပထမ call မှာ strict library ထဲက twice("21") က 42 ပြန်ပေးပါတယ် — ခေါ်တဲ့ဖိုင်က strict မဟုတ်လို့ "21" ကို ပြောင်းပေးတာပါ။ ဒါပေမယ့် sloppy(): int ကတော့ "7" ပြန်လို့ TypeError ပါ၊ ဘာလို့လဲဆိုတော့ return ကို function ကြေညာထားတဲ့ ဖိုင် (strict) နဲ့ စစ်လို့ပါ။ ဒုတိယ call မှာတော့ strict caller က strict မဟုတ်တဲ့ library function triple("5") ကို ခေါ်တာမို့ TypeError ပါ။ အကျိုးသက်ရောက်မှု အတိအကျကို သိဖို့ library တစ်ခုရဲ့ ကိုယ်ပိုင် declare ကို မကြည့်ဘဲ ခေါ်တဲ့ဖိုင်ကို ကြည့်ရပါမယ်။
Type declaration အမျိုးအစားတွေ အကျဉ်းချုပ်
| Type | PHP version | အဓိပ္ပာယ် |
|---|---|---|
| ?int | 7.1 | int ဒါမှမဟုတ် null |
| void | 7.1 | return value မရှိ |
| int|string | 8.0 | union: ဖော်ပြထားတဲ့ type တစ်ခုခု |
| mixed | 8.0 | ဘာ value မဆို |
| A&B | 8.1 | intersection: class/interface နှစ်ခုလုံး |
| never | 8.1 | function ကနေ ဘယ်တော့မှ ပြန်မလာ (exit/throw) |
ဒီ type တွေကို ရွေးတဲ့အခါ ကျယ်ပြန့်တဲ့ type (mixed) ကို နောက်ဆုံး ရွေးစရာအဖြစ်သာ သုံးပါ၊ ကတိက တိကျလေ code ဖတ်သူက ပိုယုံကြည်လေပါတယ်။ ?int နဲ့ int|null က အတူတူပါပဲ၊ ?int က ပိုတိုပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
ခေတ်သစ် PHP codebase များစွာက ဖိုင်တိုင်းရဲ့ အပေါ်ဆုံးမှာ declare(strict_types=1); ကို house rule အဖြစ် ထည့်ထားလေ့ရှိပြီး linter ဒါမှမဟုတ် coding-standard tool နဲ့ စစ်ခိုင်းထားတာ ရှိပါတယ်။ အကျိုးကတော့ type မှားတာကို production မှာမဟုတ်ဘဲ ရေးနေတုန်း (ဒါမှမဟုတ် test run တုန်း) ပေါ်လာစေတာပါ။ Static analysis tool တွေလည်း strict types ပါတဲ့ code မှာ ပိုအကျိုးရှိတဲ့ အဖြေပေးနိုင်ပါတယ်။
Strict ဖိုင်မှာ အဓိက စိန်ခေါ်မှုက input ကို boundary မှာ ပြောင်းတာပါ။ $_GET, $_POST, $_COOKIE ကနေ ရတဲ့ value တွေက string (ဒါမှမဟုတ် array) ပါ၊ ဒါကြောင့် int ကို လိုတဲ့ function ကို တိုက်ရိုက် ပို့လို့ မရပါဘူး။ filter_var($raw, FILTER_VALIDATE_INT) လို function နဲ့ ပြောင်းပြီး စစ်ကာ ရလဒ်ကို function ဆီ ပို့ပါ။ Database driver တချို့က နံပါတ်တွေကို string အဖြစ် ပြန်ပေးတတ်လို့ (driver နဲ့ setting ပေါ်မူတည်) ဒီနေရာမှာလည်း အတူတူပါပဲ။ JSON decode ကတော့ int, float, string ကို type အမှန်နဲ့ ပေးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Library ဖိုင်မှာ declare ထည့်ရင် ခေါ်သူကိုပါ ကာကွယ်မယ်လို့ ထင်တာ
strict_types=1 ကို function ကြေညာထားတဲ့ ဖိုင်မှာ ထည့်ပေမယ့် ခေါ်တဲ့ ဖိုင်မှာ မထည့်ရင် argument တွေကို ပြောင်းပေးပြီး လက်ခံပါတယ် (function body ထဲက return ကိုပဲ တင်းကျပ်စွာ စစ်ပါတယ်)။ Argument ကို တကယ် တင်းကျပ်ချင်ရင် ခေါ်မယ့် ဖိုင်တိုင်းမှာ declare ထည့်ပါ၊ ဒါမှမဟုတ် function ထဲမှာ is_int() ကဲ့သို့ ကိုယ်တိုင် စစ်ပါ။
2. declare ကို ပထမ statement မဟုတ်တဲ့နေရာမှာ ထားတာ
<?php ရဲ့ နောက်မှာ echo တစ်ကြောင်း ရေးပြီးမှ declare(strict_types=1); ရေးရင် Fatal error: strict_types declaration must be the very first statement in the script ဖြစ်ပါတယ်။ ဖိုင်ရဲ့ အပေါ်မှာ <?php မတိုင်ခင် HTML ရှိနေတာလည်း အတူတူပါပဲ။ comment တွေကိုပဲ အရင်ထားခွင့်ရှိပါတယ်။
3. Form string တွေကြောင့် TypeError တက်တာကို strict ဖြုတ်ပြီး ဖြေရှင်းတာ
$_POST['qty'] က "3" (string) ဖြစ်လို့ TypeError တက်ရင် declare ကို ဖယ်လိုက်တာက ပြဿနာကို ဖုံးကွယ်လိုက်တာပဲ ဖြစ်ပါတယ်။ ပြဿနာက input ကို မပြောင်းမစစ်ဘဲ ပို့တာမို့ filter_var() နဲ့ boundary မှာ ပြောင်းပြီး မှန်မှ function ကို ခေါ်ပါ။
4. Type မှန်ရင် တန်ဖိုးလည်း မှန်တယ်လို့ ထင်တာ
function setAge(int $age) ကို -5 ဒါမှမဟုတ် 9999 ပို့ရင် type မှန်လို့ လက်ခံပါတယ်။ Strict types က int ဟုတ်မဟုတ်ကိုပဲ စစ်ပြီး အနိမ့်ဆုံး၊ အမြင့်ဆုံး၊ ဖော်မတ်တွေကို မစစ်ပါဘူး။ ဒါတွေကို if နဲ့ စစ်ပြီး InvalidArgumentException ပစ်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ဖိုင်အသစ်တိုင်းကို <?php နောက်မှာ declare(strict_types=1); နဲ့ စပါ၊ ဖိုင်အဟောင်းတွေမှာတော့ တစ်ဖိုင်ချင်း test နဲ့အတူ ထည့်ပါ။
- User input ကို ကိုင်တဲ့နေရာ (boundary) မှာပဲ ပြောင်းပြီး စစ်ပါ၊ အတွင်း code တွေက type သန့်ရှင်းတဲ့ value တွေနဲ့ အလုပ်လုပ်ပါစေ။
- TypeError ကို production ရဲ့ မှန်ကန်တဲ့ စီးဆင်းမှုအဖြစ် မဖမ်းပါနဲ့၊ ဒါက programmer အမှားကို ပြတဲ့ signal ပါ။ User ကို ပြမယ့် error ကို validation နဲ့ ပြပါ။
- ပိုတိကျတဲ့ type ကို ရွေးပါ (?int, int|float, class name)၊ mixed ကို နောက်ဆုံးမှ သုံးပါ။
Mini challenge
Mini challenge: strict ဖိုင်ထဲက ဈေးလျှော့ စစ်ဆေးမှု
Strict file တစ်ခုမှာ ဈေးလျှော့ function ကို ရေးပါ။ Form က ရတဲ့ text ကို boundary မှာ int ဖြစ်အောင် ပြောင်းပြီး၊ type မှားတဲ့ ခေါ်မှုတွေကို TypeError ဖမ်းကာ ရှင်းရှင်းလင်းလင်း message နဲ့ ပြပါ။
- ဖိုင်ရဲ့ ပထမ statement အနေနဲ့ declare(strict_types=1) ထည့်ပါ
- discountedPrice(float $price, int $percent): float — လျှော့ပြီး ဒသမ နှစ်နေရာ round လုပ်ရမယ်
- toInt(string $raw): ?int — filter_var() နဲ့ FILTER_VALIDATE_INT သုံးပြီး မမှန်ရင် null
- tryDiscount(mixed $price, mixed $percent): string — TypeError ကို ဖမ်းပြီး ", called in" ရဲ့ အရင် အပိုင်းကို rejected (...) အဖြစ် ပြပါ
<?php
// TODO: strict mode declaration goes here
function discountedPrice(/* TODO: float price, int percent */): float
{
// TODO: price * (100 - percent) / 100, rounded to 2 decimals
}
function toInt(string $raw): ?int
{
// TODO: trim, validate with filter_var, return null on failure
}
function tryDiscount(mixed $price, mixed $percent): string
{
// TODO: call discountedPrice inside try; format with number_format
// TODO: on TypeError, return the message cut before ", called in"
}
// TODO: a valid call, a string price, a string percent
// TODO: toInt(" 25 ") feeding a discount, and toInt("12abc") rejectedရလဒ်: ထွက်လာရမယ့် output: 170.00 rejected (discountedPrice(): Argument #1 ($price) must be of type float, string given) rejected (discountedPrice(): Argument #2 ($percent) must be of type int, string given) 60.00 invalid percent
အရိပ်အမြွက် ကြည့်ရန်
tryDiscount ရဲ့ parameter တွေကို mixed လို့ ကြေညာထားလို့ ဒီနေရာမှာ type check မလုပ်ပါဘူး၊ discountedPrice ကို ခေါ်တဲ့အခါမှာ strict check ဖြစ်ပါတယ်။ strstr($message, ", called in", true) နဲ့ path ကို ဖြတ်နိုင်ပါတယ်။ filter_var() က မမှန်ရင် false ပြန်လို့ return type ?int နဲ့ ကိုက်အောင် false ကို null အဖြစ် ပြောင်းပါ။
အဖြေ ကြည့်ရန်
<?php
declare(strict_types=1);
function discountedPrice(float $price, int $percent): float
{
return round($price * (100 - $percent) / 100, 2);
}
function toInt(string $raw): ?int
{
$value = filter_var(trim($raw), FILTER_VALIDATE_INT);
return $value === false ? null : $value;
}
function tryDiscount(mixed $price, mixed $percent): string
{
try {
return number_format(discountedPrice($price, $percent), 2);
} catch (TypeError $e) {
return "rejected (" . strstr($e->getMessage(), ", called in", true) . ")";
}
}
echo tryDiscount(200, 15), "\n";
echo tryDiscount("200", 15), "\n";
echo tryDiscount(200.0, "15"), "\n";
$percent = toInt(" 25 ");
echo $percent === null ? "invalid percent" : tryDiscount(80.0, $percent), "\n";
echo toInt("12abc") === null ? "invalid percent" : "ok", "\n";Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Default (weak) mode က "5", true, 5.0, "1e2" လို ပြောင်းလို့ရတဲ့ scalar တွေကို int ဖြစ်အောင် ပြောင်းပေးပြီး "abc" နဲ့ null ကိုတော့ TypeError ပစ်ပါတယ်။
- declare(strict_types=1) က ဖိုင်ရဲ့ ပထမ statement ဖြစ်ရပြီး အဲဒီဖိုင်ထဲက call တွေမှာ exact scalar type ကိုပဲ လက်ခံပါတယ် (int→float ကပဲ ချွင်းချက်)။
- Argument ကို စစ်တာက "call ရေးတဲ့ဖိုင်" ရဲ့ setting နဲ့၊ return ကို စစ်တာက "function ကြေညာတဲ့ဖိုင်" ရဲ့ setting နဲ့ ဖြစ်ပြီး built-in function ရဲ့ callback ကိုခေါ်တာတွေက weak ပါ။
- Strict types က validation မဟုတ်ပါဘူး — form string တွေကို boundary မှာ filter_var() နဲ့ ပြောင်းစစ်ပြီး တန်ဖိုး range ကိုလည်း ကိုယ်တိုင် စစ်ပါ။
PHP Type Declarations — PHP Documentation Group