PHP 重载“="操作员 [英] PHP overload "=" operator
问题描述
有没有办法重载等号运算符?
Is there a way to overload the equals operator?
假设我有这个代码:
$variable1 = "a";
$variable1 = "c";
每次我将某些内容分配给 $variable1
时,我都想保存到日志文件中,而无需执行以下操作:
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?
推荐答案
没有.PHP 不支持运算符重载,但有一些例外(如@NikiC 所述:PHP 支持某些运算符的重载,例如 []、-> 和(字符串),并且还允许重载某些语言结构,例如 foreach").Piskvor
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
但在 http://pecl.php.net/package/operator 中是一个运算符重载的 php 扩展:+, -, *,/, %, <<, >>, ., |, &, ^, ~, !, ++, --,+=, -=, *=,/=, %=, <<=, >>=, .=, |=, &=, ^=, ~=,==、!=、===、!==、< 和 <= 运算符.对 > 和 >= 的有条件支持可通过应用补丁获得.
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.
这篇关于PHP 重载“="操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!