လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> Spl_Autoload_Unregister နှင့် Closure function ကိုအသုံးပြုနည်း

Spl_Autoload_Unregister နှင့် Closure function ကိုအသုံးပြုနည်း

gitbox 2025-05-29

PHP Development တွင်အလိုအလျောက်တင်ဆောင်သည့်ယန္တရားသည်အတန်းများ 0 န်ဆောင်မှုပေး ခြင်းနှင့်စီမံခန့်ခွဲမှုများကိုအလွန်လွယ်ကူချောမွေ့စေသည် အထူးသဖြင့်အလိုအလျောက်တင်ဆောင်လာသောအပြုအမူများကိုပုံမှန်အတိုင်းချိန်ညှိရန်လိုအပ်သည့်အခြေအနေများတွင် Spl_Autoload_Unregister ကို ပိတ်ထားခြင်းဖြင့်ပြောင်းလွယ်ပြင်လွယ်ထိန်းချုပ်နိုင်သောစွမ်းဆောင်ရည်များကိုပိုမိုပြောင်းလဲစေနိုင်သည်။ ဤဆောင်းပါးသည် Spl_Autoload_Unregister ကို မည်သို့အသုံးပြုရမည်ကိုအသေးစိတ်ဖော်ပြရန်အလိုအလျောက် 0 ယ်ယူမှု၏ပြောင်းလွယ်ပြင်လွယ်စီမံခန့်ခွဲမှုရရှိရန်အတွက်အသေးစိတ်အချက်အလက်များကိုအသေးစိတ်ဖော်ပြထားသည်။


1 ။ အလိုအလျောက်တင်ခြင်းနှင့် spl_autoload_Unregister ၏အခြေခံ

spl_autoload_register ကို autoload လုပ်ဆောင်ချက်များကိုမှတ်ပုံတင်ရန်အသုံးပြုသည်။ ပရိုဂရမ်သည်ကုန်ဆုံးသည့်အတန်းများကိုချက်ချင်းပို့သောအခါ PHP သည်ဤမှတ်ပုံတင်ထားသောလုပ်ဆောင်ချက်များကိုအတန်းဖိုင်ကိုဖွင့်ရန်ခေါ်ယူလိမ့်မည်။ spl_autoload_Unregise ကို မှတ်ပုံတင်ထားသော Autoload function ကိုထွက်ရန်အသုံးပြုသည်။ အစဉ်အလာအသုံးပြုမှုတွင်ကျွန်ုပ်တို့သည်သာမန် function names များကိုမှတ်ပုံတင်ရန်နှင့်ပယ်ဖျက်ရန်သာမန်အသုံးပြုမှုကိုအသုံးပြုကြသည်။

 function myAutoload($class) {
    include 'classes/' . $class . '.php';
}

spl_autoload_register('myAutoload');
// အချို့သောအခြေအနေများအောက်တွင် autoloading ကိုပယ်ဖျက်ပါ
spl_autoload_unregister('myAutoload');

သို့သော်အမည်မဖော်လိုသော function တစ်ခုဖြင့်အလိုအလျောက်တင်ရန်စာရင်းမှတ်ပုံတင်ခြင်းကိုအသုံးပြုသောအခါ, ပိတ်ပစ်ကို string name ဖြင့်မသတ်မှတ်နိုင်သောကြောင့်တိုက်ရိုက် Logout သည်တိုက်ရိုက်ဖြစ်သည်။


2 ။ ပိတ်သိမ်း function ကိုမှတ်ပုံတင်၏အလိုအလျောက်ဝန်တင်ခြင်း၏စိန်ခေါ်မှု

အမည်မသိ function တစ်ခုအနေဖြင့်, ပိတ်ပစ်သည်ပြင်ပ variable များကိုဖမ်းယူနိုင်ပြီးအလိုအလျောက် loxleting ကိုပိုမိုပြောင်းလွယ်ပြင်လွယ်ရှိစေသည်။

 $baseDir = '/var/www/project/classes/';

$autoload = function($class) use ($baseDir) {
    $file = $baseDir . $class . '.php';
    if (file_exists($file)) {
        include $file;
    }
};

spl_autoload_register($autoload);

ပြနာကဒီပိတ်သိမ်းမှုထဲကနေထွက်ချင်တယ်။

 spl_autoload_unregister($autoload);

ယခုအချိန်တွင်ပိတ်ထားသော variable ကို $ autoload ကို မသိမ်းဆည်းပါကအမည်မသိ function ကိုမထုတ်ပေးနိုင်ပါ။ ၎င်းသည်ဖျက်သိမ်းမှုကိုပိတ်ပစ်ရန်သော့ချက်ဖြစ်သည်။


3 ။ ပိတ်ခြင်းနှင့် spl_autoload_unregister ကို ပေါင်းစပ်ခြင်း၏ပြောင်းလွယ်ပြင်လွယ်အလေ့အကျင့်

1 ။ ပိတ်သိမ်း '' အညွှန်းကိုစောင့်ရှောက်လော့

logging လုပ်သည့်အခါအပြည့်အ 0 ပိတ်ပစ်ကိုးကားရန်လိုအပ်သည်။ ထို့ကြောင့်အလွယ်ကူဆုံးနည်းလမ်းမှာပထမ ဦး ဆုံးသိမ်းဆည်းရန်ဖြစ်သည်။

 $autoload = function($class) use ($baseDir) {
    $file = $baseDir . $class . '.php';
    if (file_exists($file)) {
        include $file;
    }
};

spl_autoload_register($autoload);

// အနာဂတ်တွင်ဖယ်ရှားရေးလိုအပ်သည်
spl_autoload_unregister($autoload);

ဤသည် logging လုပ်သည့်အခါပိတ်သိမ်း၏သမာဓိကိုသေချာစေသည်။

2 ။ ပိတ်ပစ်မှတ်ပုံတင်ခြင်းနှင့်ဖျက်သိမ်းခြင်းကိုအကောင်အထည်ဖော်ရန်၎င်းကိုစီမံခန့်ခွဲမှုလူတန်းစားသို့ encapsulate

ရှုပ်ထွေးသောစီမံခန့်ခွဲမှုများစွာ၏ရှုပ်ထွေးမှုများကိုဖြေရှင်းရန်အတွက်အတန်းတစ်ခုသည်အလိုအလျောက်ပိတ်ခြင်းနှင့်၎င်းတို့၏အပ်ပို့သပ်တို့ကို 0 န်ဆောင်မှုပေးရန်ဒီဇိုင်းပြုလုပ်နိုင်သည်။

 class AutoloadManager {
    private $loaders = [];

    public function register(callable $loader) {
        spl_autoload_register($loader);
        $this->loaders[] = $loader;
    }

    public function unregister(callable $loader) {
        spl_autoload_unregister($loader);
        $this->loaders = array_filter($this->loaders, function($l) use ($loader) {
            return $l !== $loader;
        });
    }

    public function unregisterAll() {
        foreach ($this->loaders as $loader) {
            spl_autoload_unregister($loader);
        }
        $this->loaders = [];
    }
}

အသုံးပြုမှုဥပမာ -

 $manager = new AutoloadManager();

$loader1 = function($class) {
    $file = '/path/to/dir1/' . $class . '.php';
    if (file_exists($file)) include $file;
};

$loader2 = function($class) {
    $file = '/path/to/dir2/' . $class . '.php';
    if (file_exists($file)) include $file;
};

$manager->register($loader1);
$manager->register($loader2);

// သတ်မှတ်ထားသော loader မှထွက်ပါ
$manager->unregister($loader1);

// အားလုံး loader ထွက် log
$manager->unregisterAll();

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

  • anotoading function ကိုပိတ်ပစ်လိုက်ခြင်းဖြင့်မှတ်ပုံတင်ခြင်းကိုမှတ်ပုံတင်သည့်အခါပိတ်သိမ်းရန်သင်ပိတ်ထားရမည်။

  • ပိတ်သိမ်းခြင်းကိုအချိန်မရွေးချောမွေ့စေရန်လွယ်ကူစေရန် variable ကို save ရိယာအဖြစ်သိမ်းဆည်းရန်အကြံပြုသည်။

  • Encapsulations စီမံခန့်ခွဲမှုသင်တန်းများ, ပိတ်သိမ်းမှတ်ပုံတင်ခြင်းနှင့်ဖျက်သိမ်းခြင်းကိုသုတ်ခြင်း,

  • STS_AUTOLOAD_UNREGITNESS သည် Complex Project များတွင်အလိုအလျောက်တင်ဆောင်လာသောယုတ္တိဗေဒဆိုင်ရာရွေ့လျားမှုကိုစီမံခန့်ခွဲရန်အတွက်အလိုအလျောက်တင်ဆောင်သည့်အခွင့်အလမ်းများကိုပြောင်းလဲစေနိုင်သည်။