magic-function

When do/should I use __construct(), __get(), __set(), and __call() in PHP?

邮差的信 提交于 2019-11-26 22:52:23
问题 A similar question discusses __construct, but I left it in my title for people searching who find this one. Apparently, __get and __set take a parameter that is the variable being gotten or set. However, you have to know the variable name (eg, know that the age of the person is $age instead of $myAge). So I don't see the point if you HAVE to know a variable name, especially if you are working with code that you aren't familiar with (such as a library). I found some pages that explain __get(),

Magic functions __call() for functions?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 21:57:28
问题 The magic function __call() in php are used in classes. Are there any similar magic function but for functions instead? Like __autoload() is for functions. For example something like this function __call($name, $arguments) { echo "Function $name says {$arguments[0]} "; } random_func("hello"); 回答1: Nope, I don't think such a magic function exists. One workaround for this would be to put your functions into a static class, and add a __callStatic magic method to that class (> PHP 5.3 only, I'm