PHP如何检测变量的变化? [英] PHP how to detect the change of variable?

查看:363
本文介绍了PHP如何检测变量的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP是否存在检测变量变化的函数? 就像这样:

Is PHP exists a function that detect the change of variable? That is something like this:

//called when $a is changed.
function variableChanged($value) {
    echo "value changed to " . $value;
}

$a = 1;
//attach the variable to the method.
$a.attachTo("variableChanged");
$a = 2;
$a = 3;

//expected output:
//value changed to 2
//value changed to 3

我知道如果使用"setter"方法很容易实现.但是,由于我正在处理一些现有代码,因此无法对其进行修改.有人可以告诉我如何实现自己的目标吗?谢谢.

I know that it is easy to achieve if I use the "setter" method. But since I am working on some existing codes, I am not able to modify them. Can somebody tell me how to achieve my purpose? Thanks.

推荐答案

知道如果使用"setter"方法很容易实现.但是由于我正在处理一些现有代码,所以无法对其进行修改.

know that it is easy to achieve if I use the "setter" method. But since I am working on some existing codes, I am not able to modify them.

我假定您可以更改 some 代码,但不能更改正在使用的对象/类.如果您根本无法更改任何代码,那么这个问题将毫无用处.

I assume that you can change some code, but not the object / class you are working with. If you cannot change any code at all this question would be useless.

您可以做的是建立自己的课堂,扩展您正在使用的课堂,然后在其中添加设置器.出于所有目的,除了需要跟踪的魔术设置器外,您不能覆盖父设置.跟踪更改,然后调用父功能,因此任何其他内部工作方式都不会生效.

What you can do is make your own class, extending the class you are working with, and adding your setter there. For all purposes you can not-override the parent setting, except for a magic setter on whatever you need to track. Track changes and then call the parent functions, so no changes in any other internal workings will be in effect.

这篇关于PHP如何检测变量的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆