is_a () သည်အရာဝတ်ထုတစ်ခုသည်သတ်မှတ်ထားသောအတန်းအစား၏ဥပမာတစ်ခုသို့မဟုတ်အတန်း၏ subclass ဥပမာတစ်ခုရှိမရှိဆုံးဖြတ်ရန်အသုံးပြုသည်။ function ကိုလက်မှတ်သည်အောက်ပါအတိုင်းဖြစ်သည် -
bool is_a(object $object, string $class_name, bool $allow_string = false)
$ အရာဝတ်ထု : ရှာဖွေတွေ့ရှိခံရဖို့အရာဝတ်ထု။
$ class_name : class name string ကို။
$ Allow_String (PHP 7.2+) - အရာဝတ်ထုများအစား (များသောအားဖြင့်အသုံးမ 0 င်သော) အစားအတန်းအစားအမည်ရှိကြိုးများကိုဖြတ်သန်းရန်ခွင့်ပြုသည်။
ဥပမာ -
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
if (is_a($dog, 'Animal')) {
echo "ဒါ Animal အတန်းအစားသို့မဟုတ် subclasses ၏အရာဝတ်ထု";
}
ဤတွင် ခွေးသည် တိရိစ္ဆာန် များ၏ subclass ဖြစ်နေသောကြောင့် ဖြစ်သည် ။
ပစ္စည်းဥစ်စာပိုင်ဆိုင်မှုသည် ပစ္စည်းဥစ်စာကို အများပြည်သူ , ကာကွယ်ခြင်း သို့မဟုတ် ပုဂ္ဂလိကပိုင် ဖြစ်စေ, ပစ္စည်းဥစ်စာပိုင်ဆိုင်မှုကိုကြေငြာခြင်းရှိမရှိဆုံးဖြတ်ရန်အသုံးပြုသည်။ function ကိုလက်မှတ်:
bool property_exists(object|string $class, string $property)
$ အတန်းအစား - အရာဝတ်ထုတစ်ခုသို့မဟုတ်အတန်းအစားအမည် string ကို။
$ Property : အိမ်ခြံမြေအမည် string ကို။
ဥပမာ -
class Person {
private $name;
public $age;
}
$p = new Person();
if (property_exists($p, 'name')) {
echo "အရာဝတ်ထုဂုဏ်သတ္တိများရှိသည် name";
}
if (property_exists($p, 'age')) {
echo "အရာဝတ်ထုဂုဏ်သတ္တိများရှိသည် age";
}
Property_exists () သည် လက်ရှိကုဒ်သည်ပစ္စည်း ဥစ် စာ ပိုင်ဆိုင်မှု ကိုရယူနိုင်မလားမပြောနိုင်ပါ ။ AND_A () သည် အရာဝတ်ထု၏အတန်းအစားကိုဆုံးဖြတ်ရန်နှင့် Access Permissions များကိုဆုံးဖြတ်ရန်အတန်းဖွဲ့စည်းပုံကိုပေါင်းစပ်နိုင်သည်။
သို့သော် PHP ကိုယ်တိုင်ကပိုင်ဆိုင်မှုတစ်ခု၏ "0 င်ရောက်မှုလုံခြုံမှု" ကိုဆုံးဖြတ်ရန်တိုက်ရိုက်လုပ်ဆောင်ချက်မရှိပါ။ ပုံမှန်လေ့ကျင့်မှုမှာ
ပထမ ဦး ဆုံးအသုံးပြုမှု is_a () အရာဝတ်ထုအချို့သောထိန်းချုပ်မှုလူတန်းစားအကွာအဝေးပိုင်ရှိမရှိဆုံးဖြတ်ရန်။
ထို့နောက်ပစ္စည်းဥစ်စာပိုင်ဆိုင်မှုတည်ရှိမှုရှိမရှိဆုံးဖြတ်ရန် Propert_Exists () ကိုသုံးပါ။
နောက်ဆုံးတွင် Access interface သည်ပစ္စည်းဥစ်စာပိုင်ဆိုင်မှု၏လုံခြုံရေးကိုသေချာစေရန်ရောင်ပြန်ဟပ်မှုသို့မဟုတ်ဒီဇိုင်းများကိုသတ်မှတ်ခြင်းဖြင့်သတ်မှတ်ထားသည်။
<?php
class User {
public $username;
protected $email;
private $password;
public function __construct($username, $email, $password) {
$this->username = $username;
$this->email = $email;
$this->password = $password;
}
}
function checkPropertyAccess($obj, $property, $allowedClass) {
// အရာဝတ်ထုတစ်ခုသည်လူတန်းစားတစ် ဦး သို့မဟုတ်၎င်း၏ subclass ရှိမရှိဆုံးဖြတ်ရန်ခွင့်ပြုထားသည်ကိုဆုံးဖြတ်ပါ
if (!is_a($obj, $allowedClass)) {
return false;
}
// attribute ကိုတည်ရှိရှိမရှိဆုံးဖြတ်ရန်
if (!property_exists($obj, $property)) {
return false;
}
// attribute တွေရဲ့မြင်ကွင်းကိုဆုံးဖြတ်ရန်ရောင်ပြန်ဟပ်မှုကိုသုံးပါ
$reflection = new ReflectionObject($obj);
if (!$reflection->hasProperty($property)) {
return false;
}
$prop = $reflection->getProperty($property);
// attribute ကိုရှိမရှိဆုံးဖြတ်ပါpublic
if ($prop->isPublic()) {
return true;
}
// မရရှိလျှင်public,ဆိုလိုသည်မှာသင်သည်တိုက်ရိုက် ဆက်သွယ်. မရပါ,ဝင်ရောက်ခွင့်၏လုံခြုံရေးကန့်သတ်
return false;
}
$user = new User('alice', 'alice@example.com', 'secret');
var_dump(checkPropertyAccess($user, 'username', 'User')); // true
var_dump(checkPropertyAccess($user, 'email', 'User')); // false
var_dump(checkPropertyAccess($user, 'password', 'User')); // false
var_dump(checkPropertyAccess($user, 'nonexist', 'User')); // false
var_dump(checkPropertyAccess(new stdClass(), 'username', 'User')); // false
is_a () သည် Access scope ၏လုံခြုံရေးကိုသေချာစေရန်အရာဝတ်ထုတစ်ခုသို့မဟုတ်၎င်း၏ subclass တစ်ခု၏ပိုင်ဆိုင်မှုတစ်ခုရှိမရှိဆုံးဖြတ်ရန်အသုံးပြုသည်။
အိမ်ခြံမြေ 0 န်းကျင် () သည်အရာဝတ်ထုသည်သတ်မှတ်ထားသောပစ္စည်းများကိုကြေငြာခြင်းရှိမရှိရှာဖွေရန်အသုံးပြုသည်။
ရောင်ပြန်ဟပ်မှုယန္တရားကိုပေါင်းစပ်ပြီး attribute များ၏ attribute များ၏အခွင့်အရေးများကိုဆက်လက်ဆုံးဖြတ်ခြင်းနှင့်ပုဂ္ဂလိကသို့မဟုတ်ကာကွယ်ထားသော attribute များသို့ရှောင်ရှားနိုင်သည်။
အမှန်တကယ်ဖွံ့ဖြိုးတိုးတက်မှုတွင်အိမ်ခြံမြေ 0 င်ရောက်မှုလုံခြုံမှု၏လုံခြုံရေးကိုသေချာစေရန်,
ဤလုပ်ဆောင်မှုများကိုကျိုးကြောင်းဆီလျော်စွာအသုံးပြုခြင်းအားဖြင့် code ၏ကြံ့ခိုင်မှုနှင့်လုံခြုံရေးကိုထိရောက်စွာတိုးတက်အောင်လုပ်နိုင်သည်, Runtime အမှားများနှင့်လုံခြုံရေးအန္တရာယ်များကိုလျှော့ချနိုင်သည်။