问题
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