__callStatic Method

__callStatic method သည် PHP တွင် မှော်ဆန်သော method တစ်ခုဖြစ်ပြီး၊ မရှိသော သို့မဟုတ် မရနိုင်သော static method တစ်ခုကို ခေါ်ဆိုရန် ကြိုးပမ်းသည့်အခါ အလိုအလျောက်ခေါ်ဆိုခြင်း ခံရသည်။ ၎င်းသည် ပထမပါရာမီတာအနေဖြင့် ခေါ်ဆိုသည့် method ၏အမည်ကို လက်ခံပြီး၊ ဒုတိယပါရာမီတာအနေဖြင့် ပေးပို့သည့် argument များ၏ array ကို လက်ခံသည်။

Syntax

phppublic static function __callStatic(string $name, array $arguments);

ဥပမာ

__callStatic method ပါရှိသော class တစ်ခုကို ဖန်တီးကြပြီး၊ မရှိသော static method တစ်ခုကို ခေါ်ဆိုရန် ကြိုးပမ်းကြည့်ကြပါစို့။

php<?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 လိုက်သည့်အခါ ရရှိသော ရလဒ်။

phpCalled static method 'nonExistentMethod' with arguments: [1, 2, 3]

ဥပမာ

မတူညီသော အမျိုးအစားများ၏ object များကို ဖန်တီးရန် အလွယ်ကူဆုံး facade တစ်ခုကို အကောင်အထည်ဖော်ကြပါစို့။

php<?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 လိုက်သည့်အခါ ရရှိသော ရလဒ်။

phptrue

ဒါကိုလည်း ကြည့်ပါ

  • __call method,
    အရာဝတ္ထု၏ မရှိသော method များခေါ်ဆိုမှုကို လက်ခံရရှိခြင်း
  • __get method,
    မရှိသော property များသို့ ဆက်သွယ်မှုကို လက်ခံရရှိခြင်း