PHP overload “=” operator

梦想的初衷 提交于 2019-12-24 02:41:13

问题


Is there a way to overload the equals operator?

Let's say I have this code:

$variable1 = "a";
$variable1 = "c";

I would like to save to a log file everytime i assign something to $variable1 without having to do something like:

$variable1 = "a";
add_to_some_logfile("a");

$variable1 = "c";
add_to_some_logfile("c");

Is there a way to override the equals operator in order to do some other operation other than just assigning the value to the variable?


回答1:


No. PHP doesn't support operator overloading, with a few exceptions (as noted by @NikiC: "PHP supports overloading of some operators, like [], -> and (string) and also allows overloading some language constructs like foreach").Piskvor

php overload = operator

but in http://pecl.php.net/package/operator is a php extension that Operator overloading for: +, -, *, /, %, <<, >>, ., |, &, ^, ~, !, ++, --, +=, -=, *=, /=, %=, <<=, >>=, .=, |=, &=, ^=, ~=, ==, !=, ===, !==, <, and <= operators. Conditional support for > and >= available with application of a patch.



来源:https://stackoverflow.com/questions/15953591/php-overload-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!