စီမံကိန်းတစ်ခုသို့တတိယပါတီစာကြည့်တိုက်များသို့မဟုတ်မူဘောင်မျိုးစုံကိုစီမံကိန်းတစ်ခုသို့မိတ်ဆက်ပေးသည်ဆိုပါစို့။
spl_autoload_register(function ($class) {
// ပထမ ဦး ဆုံး autoloader,စီမံကိန်းကိုတင်ရန်အတွက်တာဝန်ရှိသည်သင်တန်း
$file = __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
});
spl_autoload_register(function ($class) {
// ဒုတိယ autoloader,တတိယပါတီစာကြည့်တိုက်များကိုဖွင့်သည့်အတန်းများ
$file = __DIR__ . '/vendor/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
});
ဤ autoloaders နှစ်ခုကို PHP မှအလှည့်အပြောင်းဖြင့်ပုံမှန်အားဖြင့်ပုံမှန်အလုပ်လုပ်သော class file ကိုဖွင့်ရန်ကြိုးစားလိမ့်မည်။ သို့သော်အလိုအလျှောက် loader ၏ loader loader ၏ loader 0 တ်ထုနှင့်ပြ a နာရှိပါက၎င်းသည်ချွင်းချက်တစ်ခုသို့မဟုတ်သေနေသောကွင်းဆက်ကိုလွှင့်ပစ်ခြင်းကဲ့သို့နောက်ဆက်တွဲ loader ကိုမလုပ်ဆောင်နိုင်အောင်ပ conflict ိပက်ခဖြစ်စေနိုင်သည်။
spl_autoload_register - autoload တန်းစီသို့ autoload function အသစ်ကိုမှတ်ပုံတင်ပါ။ ၎င်းကိုလက်ရှိ autoload function list အဆုံးတွင်ဖြည့်ဆည်းပေးလိမ့်မည်။
spl_autoload_Unregise - function ကိုခေါ်ခြင်းမှကာကွယ်ရန် Autoload Quick မှ Autooload function မှ Autooload function မှထွက်ပါ။
ဤလုပ်ဆောင်မှုနှစ်ခုမှတဆင့်ပ conflicts ိပက်ခများကိုရှောင်ရှားရန် autooloader ကိုရွေးချယ်နိုင်သည်။
autoloader သည်အခြား autooloaders များနှင့်ပ conflict ိပက်ခဖြစ်လိမ့်မည်ဟုယူဆသည်, ၎င်းကိုခေါ်ဆိုခြင်းနှင့်ခေါ်ဆိုခြင်းမပြုမီပြန်လည်မှတ်ပုံတင်ခြင်းမပြုမီကျွန်ုပ်တို့ log ပြုလုပ်နိုင်သည်။
// 定义ပထမ ဦး ဆုံး autoloader
$loader1 = function ($class) {
$file = __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
};
// 定义ဒုတိယ autoloader
$loader2 = function ($class) {
$file = __DIR__ . '/vendor/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
};
// နှစ်ခု autoloaders နှစ်ခုမှတ်ပုံတင်ပါ
spl_autoload_register($loader1);
spl_autoload_register($loader2);
// ကုဒ်အချို့အပိုင်းအစကွပ်မျက်ခံရသောအခါ,我们暂时注销ဒုတိယ autoloader,ပ conflicts ိပက်ခများကိုရှောင်ပါ
spl_autoload_unregister($loader2);
// 这里执行只依赖ပထမ ဦး ဆုံး autoloader的代码
$obj = new SomeClassFromSrc();
// Execution ပြီးစီးခဲ့သည်,重新注册ဒုတိယ autoloader
spl_autoload_register($loader2);
// နောက်ဆက်တွဲကုဒ်သည် autoloaders နှစ်ခုကိုဆက်လက်အသုံးပြုနိုင်သည်
$obj2 = new SomeClassFromVendor();
ဤအရာ၏အားသာချက်သည်တစ်ချိန်တည်းမှာပင်ပ conflicts ိပက်ခများကြောင့်ပ conflicts ိပက်ခများကြောင့်ဖြစ်ပေါ်လာသောပြ problems နာများကိုရှောင်ရှားရန်အလိုအလျောက် loader ၏ starter ၏စတင်ခြင်းနှင့်ရပ်တန့်ရန်ဖြစ်သည်။
Autoload Autoload ပတ် 0 န်းကျင်တွင်အလိုအလျောက် loader ပ conflicts ိပက်ခများပိုမိုများပြားသည်။ PHP's Spl_autoload_Unregister နှင့် spl_autoload_register ကိုအသုံးပြုခြင်းသည်မတူညီသော module များ၏ loading logic 0 င်ရောက်စွက်ဖက်ခြင်းမပြုရန်အလိုအလျောက် loader ၏စွမ်းဆောင်ရည်အခြေအနေကိုအသုံးပြုရန်ပြောင်းလဲနိုင်သည်။
အကယ်. သင်သည်အမှန်တကယ်ဖွံ့ဖြိုးတိုးတက်မှုတွင်အလားတူပြ problems နာများကြုံတွေ့ရပါကသင်၏ကုဒ်များကိုချောချောမွေ့မွေ့ပြုလုပ်ရန်သေချာစေရန်အထက်ပါအကြံဥာဏ်များကိုသင်ကြည့်ရှုနိုင်သည်။
// နမူနာကုဒ်
$loader1 = function ($class) {
$file = __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
};
$loader2 = function ($class) {
$file = __DIR__ . '/vendor/' . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
};
spl_autoload_register($loader1);
spl_autoload_register($loader2);
// 需要只使用ပထမ ဦး ဆုံး autoloader时
spl_autoload_unregister($loader2);
$obj = new SomeClassFromSrc();
spl_autoload_register($loader2);
$obj2 = new SomeClassFromVendor();