__callStatic ක්රමය
__callStatic ක්රමය PHP හි මැජික් ක්රමයක් වන අතර, එය නොපවතින හෝ ලබා ගත නොහැකි ස්ථිතික ක්රමයක් ඇමතීමට උත්සාහ කිරීමේදී ස්වයංක්රීයව ඇමතෙයි. පළමු පරාමිතිය ලෙස එය ඇමතෙන ක්රමයේ නම පිළිගන්නා අතර, දෙවන පරාමිතිය ලෙස යවන ලද තර්ක අරාව පිළිගනී.
වාක්ය රචනය
public static function __callStatic(string $name, array $arguments);
උදාහරණය
__callStatic ක්රමය සහිත පංතියක් සාදා නොපවතින ස්ථිතික ක්රමයක් ඇමතීමට උත්සාහ කරමු:
<?php
class MyClass {
public static function __callStatic($name, $args) {
echo "Called static method '$name' with arguments: ";
print_r($args);
}
}
MyClass::nonExistentMethod(1, 2, 3);
?>
කේතය ක්රියාත්මක කිරීමේ ප්රතිඵලය:
Called static method 'nonExistentMethod' with arguments: [1, 2, 3]
උදාහරණය
විවිධ වර්ගවල වස්තු සෑදීම සඳහා සරල අංශයක් ක්රියාත්මක කරමු:
<?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);
?>
කේතය ක්රියාත්මක කිරීමේ ප්රතිඵලය:
true