Thuta Learning
PHP
IntermediateProgrammingbeginner

Type Declarations & Strict Types

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

  • Type Declarations & Strict Types ရဲ့PHP runtime နဲ့web behavior ကိုရှင်းပြနိုင်ရန်
  • Normal, boundary, invalid နဲ့failure cases စမ်းနိုင်ရန်
  • Secure, maintainable PHP code ရေးနိုင်ရန်

Strict file မှခေါ်သောcall တွင်coercible scalar arguments ကိုstrict mode ကreject လုပ်ပါတယ်။

ပိုပြီးနားလည်ထားရမယ့် အချက်

PHP type declarations တွင်scalar, nullable, union, intersection နဲ့class types ပါနိုင်ပါတယ်။ `declare(strict_types=1)` သည်scalar calls အတွက်file-by-file caller behavior ကိုပြောင်းတာဖြစ်ပြီးinput validation ကိုအစားမထိုးပါ။

လက်တွေ့မှာ ဘယ်လိုအသုံးချမလဲ

Type Declarations & Strict Types example ကိုnormal, boundary, invalid နဲ့failure inputs ဖြင့်run/test လုပ်ပြီး output, warnings, exceptions နဲ့side effects ကိုမှတ်တမ်းတင်ပါ။

ဒီသင်ခန်းစာပြီးရင်

php
<?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);
You should see
58.5

ကိုယ်တိုင်စမ်းကြည့်ရန်

Type Declarations & Strict Types example ကိုnormal, boundary, invalid နဲ့failure inputs ဖြင့်run/test လုပ်ပြီး output, warnings, exceptions နဲ့side effects ကိုမှတ်တမ်းတင်ပါ။

Security/Common Mistake သတိပေးချက်

Strict mode သည်declaration file မဟုတ်ဘဲcall လုပ်သည့်file ၏setting အပေါ်မူတည်ပါတယ်။

PHP Type DeclarationsPHP Documentation Group

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Strict mode သည်declaration file မဟုတ်ဘဲcall လုပ်သည့်file ၏setting အပေါ်မူတည်ပါတယ်။
  • Sample output တစ်ခုမှန်ရုံဖြင့် input, output, authorization နဲ့failure paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Type Declarations & Strict Types example ကိုnormal, boundary, invalid နဲ့failure inputs ဖြင့်run/test လုပ်ပြီး output, warnings, exceptions နဲ့side effects ကိုမှတ်တမ်းတင်ပါ။

You'll know it worked when: 58.5

Type Declarations & Strict Types | Thuta Learning