လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> Laravel ၏မှီခိုသောဆေးထိုးကွန်တိန်နာနှင့်အတူ is_a () ကိုအသုံးပြုသည်

Laravel ၏မှီခိုသောဆေးထိုးကွန်တိန်နာနှင့်အတူ is_a () ကိုအသုံးပြုသည်

M66 2025-06-06

Laravel framework ကို အသုံးပြု. application များဖွံ့ဖြိုးဆဲသောအခါ, Laravel ၏မှီခိုမှုဆေးထိုးခြင်း function သည်ကုဒ်၏စစ်ဆေးမှုကိုတိုးတက်စေသည်သာမကထိန်းသိမ်းမှုကိုများစွာတိုးတက်စေသည်။ သို့သော်အချို့သောအခြေအနေများတွင်မှီခိုနေရသောအရာဝတ်ထုအမျိုးအစားကိုကျွန်ုပ်တို့ဆုံးဖြတ်ရန်လိုအပ်နိုင်သည်။ ဤအချိန်တွင် is_a () function သည်အလွန်အသုံးဝင်သည်။

1 ။ is_a () function ကိုမိတ်ဆက်

PHP ၏ built-in function ကို is_a () is_a ()) သည် သတ်မှတ်ထားသောလူတန်းစားသို့မဟုတ်အတန်း၏ subclass ၏ဥပမာတစ်ခုရှိမရှိဆုံးဖြတ်ရန်အသုံးပြုသည်။

 is_a(object|string $object_or_class, string $class, bool $allow_string = false): bool

parameter သည်ဖော်ပြချက်:

  • $ Office_or_Class : စစ်ဆေးရန်အရာဝတ်ထုသို့မဟုတ်အတန်းအစားအမည်။

  • $ အတန်း : ပစ်မှတ်အတန်းအစားအမည်။

  • $ Allow_String : Class Name string ကိုခွင့်ပြု ထား,

2 ။ 0 န်ဆောင်မှုကွန်တိန်နာတွင်အမျိုးအစားစစ်ဆေးခြင်းကိုအဘယ်ကြောင့်အသုံးပြုသနည်း။

တခါတရံတွင်ကျွန်ုပ်တို့သည်ကွဲပြားခြားနားသော interfaces သို့မဟုတ်မိဘအတန်းများအပေါ် အခြေခံ. အကောင်အထည်ဖော်မှုသင်တန်းများကိုမှတ်ပုံတင်ရန်လိုအပ်သည်။ ဥပမာအားဖြင့်, သင်၌တူညီသော interface ကိုအကောင်အထည်ဖော်မည့်ငွေပေးချေမှုဂိတ်ဝေး 0 န်ဆောင်မှုမျိုးစုံရှိနိုင်သည်။ ယခုအချိန်တွင် is_a () ကို အသုံးပြုခြင်းသည်ကွန်တိန်နာထဲသို့ 0 န်ဆောင်မှုပေးသည့်အမျိုးအစားကိုဆုံးဖြတ်ရန်သင့်အားဆုံးဖြတ်ရန်အသုံးပြုသည်။

3 ။ 0 န်ဆောင်မှုပေးသူများတွင် 0 န်ဆောင်မှုပေးသူများကို is_a နှင့်ပေါင်းစပ်။

ငွေပေးချေမှုတံခါးပေါက်တစ်ခု၏ဥပမာတစ်ခုဖြင့်သရုပ်ဖော်ပါရစေ။

ပထမ ဦး ဆုံး interface ကို ငွေပေးချေမှု waymatewarewareinterface ကို သတ်မှတ်

 namespace App\Contracts;

interface PaymentGatewayInterface
{
    public function charge(float $amount);
}

ထို့နောက်အကောင်အထည်ဖော်မှုသင်တန်းနှစ်ခုကိုသတ်မှတ်ပါ။

 namespace App\Services;

use App\Contracts\PaymentGatewayInterface;

class StripeGateway implements PaymentGatewayInterface
{
    public function charge(float $amount)
    {
        // Stripe တက်ဏဗေဒ
    }
}

class PaypalGateway implements PaymentGatewayInterface
{
    public function charge(float $amount)
    {
        // Paypal တက်ဏဗေဒ
    }
}

0 န်ဆောင်မှုပေးသူတစ် ဦး တွင်ကျွန်ုပ်တို့သည်အတန်းသည်အမျိုးအစားတစ်ခုနှင့်သက်ဆိုင်ခြင်းရှိ,

 namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Contracts\PaymentGatewayInterface;
use App\Services\StripeGateway;
use App\Services\PaypalGateway;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(PaymentGatewayInterface::class, function ($app) {
            $gateway = config('services.payment_gateway'); // ဒီ configuration ကို item ကို class name string ကိုယူဆ

            if (is_a($gateway, PaymentGatewayInterface::class, true)) {
                return new $gateway();
            }

            throw new \InvalidArgumentException("မမှန်ကန်သောငွေပေးချေသည့် Gateway အမျိုးအစား:{$gateway}");
        });
    }
}

configuration file file config / services.php ကို အောက်ပါအတိုင်းသတ်မှတ်ထားသည်ဟုယူဆကြပါစို့။

 return [
    'payment_gateway' => \App\Services\StripeGateway::class,
];

၎င်းသည်မတူကွဲပြားသောအကောင်အထည်ဖော်မှုသင်တန်းများကို configurations is_a () မှတစ်ဆင့်ပြောင်းလဲခြင်းကိုခွင့်ပြုရန်ခွင့်ပြုသည်,

4 ။ ခွဲခြမ်းစိတ်ဖြာခြင်းနှင့်ကွန်တိန်နာမှ၎င်းကိုသုံးပါ

Controller သို့မဟုတ် Service Class တွင် Type Prompt Interface ကိုသာလိုအပ်သည်။

 use App\Contracts\PaymentGatewayInterface;

class OrderController extends Controller
{
    public function pay(PaymentGatewayInterface $gateway)
    {
        $gateway->charge(100);
    }
}

Laravel သည် configuration တွင်သတ်မှတ်ထားသောအကောင်အထည်ဖော်မှုသင်တန်းများကိုအလိုအလျောက်ထိုးသွင်းလိမ့်မည်

5 ။ အကျဉ်းချုပ်

is_a () သည်ရိုးရှင်းသော်လည်းအလွန်လက်တွေ့ကျတဲ့ကိရိယာ function တစ်ခုဖြစ်သည်။ ၎င်းကို Laravel ၏ 0 န်ဆောင်မှုကွန်တိန်နာတွင်အသုံးပြုခြင်းသည်အမျိုးအစားတရားစီရင်ခြင်းယုတ္တိဗေဒ၏လုံခြုံရေးနှင့်ပြောင်းလွယ်ပြင်လွယ်မှုကိုမြှင့်တင်နိုင်သည်။ အထူးသဖြင့်ဖွဲ့စည်းပုံသို့မဟုတ် runtime အခြေအနေများအပေါ် အခြေခံ. Binding 0 န်ဆောင်မှုများဖြစ်ရပ်များကိုဆုံးဖြတ်ရန်လိုအပ်ပါက is_a () သည် type validation ကိုလုပ်ဆောင်ရန်ကြော့ရှင်းသောနည်းလမ်းဖြစ်သည်။

ဤနည်းအားဖြင့်ကွန်တိန်နာမှထိုးသွင်းထားသော 0 န်ဆောင်မှုများသည်မျှော်မှန်းထားသော interface ကန်ထရိုက်နှင့်တွေ့ဆုံရန်,