PHP တွင်, ကျွန်ုပ်တို့သည် Runtime တွင် 0 င်ရောက်ခြင်းသို့မဟုတ်ကြိုးကိုင်မှုများပြုလုပ်သောအခါအမျိုးအစားအမှားမရှိကြောင်းသေချာစေရန်အရာဝတ်ထု၏ဂုဏ်သတ္တိများကိုစစ်ဆေးရန်လိုအပ်သည်။ PHP သည် dynamic language ဖြစ်ပြီးရှုပ်ထွေးသောအရာဝတ်ထုများသို့မဟုတ် interfaces များနှင့်အပြန်အလှန်ဆက်သွယ်မှုကိုအပြန်အလှန်ဆက်သွယ်သည့်အခါ attribute များတည်ရှိခြင်းရှိ,
ဤဆောင်းပါးသည်အရာဝတ်ထုဂုဏ်သတ္တိများကိုတိကျစွာတရားစီရင်နိုင်ရန်အတွက်မည်သည့် အိမ်ခြံမြေ 0 န်းကျင် နှင့် Get_Class () functions များကိုမည်သို့အသုံးပြုရမည်ကိုမိတ်ဆက်ပေးလိမ့်မည်။
Polymorphic Object Office အဆောက်အအုံများပေါ်တွင်မှီခိုနေသောစီးပွားရေးစနစ်တစ်ခုကိုသင်တီထွင်နေသည်ဟုဆိုပါစို့။ ဥပမာအားဖြင့်:
class User {
public Profile $profile;
}
class Admin {
public AdminProfile $profile;
}
class Guest {
// မဟုတ် profile ဉစ်စာပစ္စည်းများ
}
မည်သည့်အရာဝတ်ထုတွင်မဆိုဖြတ်သန်းလိုပြီး၎င်းတွင် ပရိုဖိုင်း attribute နှင့်အမျိုးအစားအန္တရာယ်ကင်းရှင်းရေးမပါသော attribute ၏တိကျသောလူတန်းစားအမည်ရှိ,
အိမ်ခြံမြေ 0 န်းကျင် () သည် PHP built-in function တစ်ခုဖြစ်သည်။ ၎င်းသည်အရာဝတ်ထုတစ်ခုသို့မဟုတ်အတန်းထဲတွင်ပစ္စည်းတစ်ခုရှိ, Syntax သည်အောက်ပါအတိုင်းဖြစ်သည် -
bool property_exists(object|string $object_or_class, string $property)
ဥပမာ -
if (property_exists($object, 'profile')) {
// ဖော်ပြချက်အရာဝတ်ထုအတွက်သတ်မှတ် profile ဉစ်စာပစ္စည်းများ
}
ဒါပေမယ့်သတိပြုပါ, အဲဒါကပစ္စည်းဥစ်စာကိုအစပြုမလားဆိုတာကိုမဆုံးဖြတ်ဘူးဆိုတာကိုသတိပြုပါ။
အရာဝတ်ထုတွင်ဤပစ္စည်းဥစ်စာပိုင်ဆိုင်မှုရှိကြောင်းအတည်ပြုသည်နှင့်တပြိုင်နက်ကျွန်ုပ်တို့သည်တန်ဖိုး၏တိကျသောအမျိုးအစားကိုဆုံးဖြတ်ရန်အတွက်၎င်း၏တန်ဖိုးကိုရယူရန်နှင့် Get_Class () ကိုသုံးနိုင်သည်။
if (property_exists($object, 'profile')) {
$profileValue = $object->profile;
if (is_object($profileValue)) {
$profileClass = get_class($profileValue);
echo "ဉစ်စာပစ္စည်းများ profile အမျိုးအစား: $profileClass";
} else {
echo "ဉစ်စာပစ္စည်းများ profile တည်နေ,ဒါပေမယ့်မဟုတ်ပါဘူး。";
}
}
ဤတွင်အရာဝတ္ထုများကိုလက်ခံသောလက်တွေ့လုပ်ဆောင်မှုကိုလက်ခံပြီး၎င်းတို့သည်သတ်မှတ်ထားသော attribute များနှင့် output များကိုသတ်မှတ်ခြင်းရှိမရှိဆုံးဖြတ်ရန်ကြိုးစားသည်။
function getObjectPropertyClass(object $object, string $property): ?string {
if (property_exists($object, $property)) {
$value = $object->$property ?? null;
if (is_object($value)) {
return get_class($value);
}
}
return null;
}
အသုံးပြုမှုဥပမာ -
$user = new User();
$user->profile = new Profile();
$className = getObjectPropertyClass($user, 'profile');
if ($className) {
echo "profile အတန်းအစားအမည်:$className";
} else {
echo "该ဉစ်စာပစ္စည်းများ不တည်နေ或不是对象。";
}
PHP 7.4+ တွင်ပစ္စည်းဥစ်စာကိုရိုက်ထည့်ပြီးတာ 0 န်ယူလျှင်၎င်းကိုတိုက်ရိုက်ကြည့်ခြင်းအားဖြင့်အမှားတစ်ခုပြုလုပ်သည်။ ပစ္စည်းဥစ်စာကိုအစပျိုးခြင်းရှိမရှိကိုလျောက်ပတ်စွာဆုံးဖြတ်ရန် ပြန်လည်ပြုပြင်ခြင်းကို သင်သုံးနိုင်သည်။
function isPropertyInitialized(object $object, string $property): bool {
try {
$ref = new ReflectionProperty($object, $property);
return $ref->isInitialized($object);
} catch (ReflectionException $e) {
return false;
}
}
ပေါင်းစပ်အသုံးပြုပါ
if (property_exists($object, 'profile') && isPropertyInitialized($object, 'profile')) {
$profile = $object->profile;
if (is_object($profile)) {
echo "အမျိုးအစားဖြစ်ပါတယ်:" . get_class($profile);
}
}
ဥပမာအားဖြင့်, သင်သည်ဒေတာအရာဝတ်ထုကိုပြုပြင်သည့်အခါ Remote API မှပြန်လာသောအခါ (ဥပမာ, https://api.gitbox.net/users/123 ), အထက်ဖော်ပြပါနည်းလမ်းကိုသင် ပရိုဖိုင်း နှင့် ချိန်ညှိချက်များ ကဲ့သို့သောဂုဏ်သတ္တိများမရှိမရှိကို dynonically ကိုသုံးနိုင်သည်။
ဤချဉ်းကပ်မှုသည်အရာဝတ်ထုအဆောက်အအုံများမဟုတ်သည့်ဒေတာဆိုင်ရာ interfaces များကို deserialization သို့မဟုတ် processing interfaces များအတွက်အကောင်းဆုံးဖြစ်သည်။
Property_exists () နှင့် get_class () ကိုပေါင်းစပ်ခြင်း) သည်အဆင့်မြင့်ဆိုင်ရာ ပြန်လည်ထူထောင်ခြင်း အသုံးချမှုနှင့်ပေါင်းစပ်ပြီး, ဤချဉ်းကပ်မှုသည်ဒေတာမောင်းနှင်မှုဒီဇိုင်းနှင့်ဆက်စပ်သောပရိုဂရမ်းမင်းများ, interface response processing သို့မဟုတ်ရောင်ပြန်ဟပ်မှုနှင့်ဆက်စပ်သောပရိုဂရမ်းမင်းဖြစ်ရပ်များအတွက်အထူးသင့်တော်သည်။ Code ၏ကြံ့ခိုင်မှုနှင့်ထိန်းသိမ်းခြင်းကိုတိုးတက်စေရန်အလွန်လက်တွေ့ကျတန်ဖိုးရှိသည်။