__callStatic Method
__callStatic method သည် PHP တွင် မှော်ဆန်သော method တစ်ခုဖြစ်ပြီး၊ မရှိသော သို့မဟုတ် မရနိုင်သော static method တစ်ခုကို ခေါ်ဆိုရန် ကြိုးပမ်းသည့်အခါ အလိုအလျောက်ခေါ်ဆိုခြင်း ခံရသည်။ ၎င်းသည် ပထမပါရာမီတာအနေဖြင့် ခေါ်ဆိုသည့် method ၏အမည်ကို လက်ခံပြီး၊ ဒုတိယပါရာမီတာအနေဖြင့် ပေးပို့သည့် argument များ၏ array ကို လက်ခံသည်။
Syntax
public static function __callStatic(string $name, array $arguments);
ဥပမာ
__callStatic method ပါရှိသော class တစ်ခုကို ဖန်တီးကြပြီး၊ မရှိသော static method တစ်ခုကို ခေါ်ဆိုရန် ကြိုးပမ်းကြည့်ကြပါစို့။
<?php
class MyClass {
public static function __callStatic($name, $args) {
echo "Called static method '$name' with arguments: ";
print_r($args);
}
}
MyClass::nonExistentMethod(1, 2, 3);
?>
ကုဒ်ကို run လိုက်သည့်အခါ ရရှိသော ရလဒ်။
Called static method 'nonExistentMethod' with arguments: [1, 2, 3]
ဥပမာ
မတူညီသော အမျိုးအစားများ၏ object များကို ဖန်တီးရန် အလွယ်ကူဆုံး facade တစ်ခုကို အကောင်အထည်ဖော်ကြပါစို့။
<?php
class Factory {
public static function __callStatic($name, $args) {
if (strpos($name, 'create') === 0) {
$className = substr($name, 6);
return new $className(...$args);
}
throw new Exception("Method $name not found");
}
}
class User {}
$user = Factory::createUser();
var_dump($user instanceof User);
?>
ကုဒ်ကို run လိုက်သည့်အခါ ရရှိသော ရလဒ်။
true