我想知道是否有办法将变量侦听器添加到变量中。我所说的最简单的例子就是沿着这些方向发展;PHP:检测变量值何时发生变化
// Start with a variable
$variable = "some value";
// Define a listener
function myChangeListener($variable) {
// encode with json_encode and send in cookie
}
// Add my listener to the variable
addListenerToVariable(&$variable, 'myChangeListener');
// Change the variables value, triggering my listener
$variable = "new value";
现在你可能会问为什么在世界上我甚至会需要使用这种方法,为什么不只是做一个函数来设置cookie的打扰。答案是我有一个&__get($var)
神奇的方法,返回一个多维数组元素的引用,我希望使用这样的东西来检测何时/如果数组元素或其子元素之一已被编辑,然后如果有,发送一个cookie。希望得到的结果会像这样工作;
$cookies->testArray["test"] = "newValue";
// This would send the cookie 'testArray' with the value '{"test":"newValue"}'
我诚实地希望这是完全不可能的,如果我是正确的,我很抱歉。但是我刚刚学会了如何正确地使用引用,所以我想在我完全注销这个想法之前我会问。
感谢您收到的任何回复,无论是我期望的还是我期望的。
编辑:
为了增加清晰度,这里是为了什么,我试图完成的示例类;
class MyCookies {
private $cookies = array();
private $cookieTag = "MyTag";
public function __construct() {
foreach($_COOKIE as $cookie => $value) {
if(strlen($cookie)>strlen($this->cookieTag."_")&&substr($cookie,0,strlen($this->cookieTag."_"))==$this->cookieTag."_") {
$cookieVar = substr($cookie,strlen($this->cookieTag."_"));
$this->cookies[$cookieVar]['value'] = json_decode($value);
}
}
}
// This works great for $cookies->testArray = array("testKey" => "testValue");
// but never gets called when you do $cookies->testArray['testKey'] = "testValue";
public function __set($var, $value) {
if(isset($value)) {
$this->cookies[$var]['value'] = $value;
setcookie($this->cookieTag.'_'.$var,json_encode($value),(isset($this->cookies[$var]['expires'])?$this->cookies[$var]['expires']:(time()+2592000)),'/','');
} else {
unset($this->cookies[$var]);
setcookie($this->cookieTag.'_'.$var,'',(time()-(172800)),'/','');
}
return $value;
}
// This gets called when you do $cookies->testArray['testKey'] = "testValue";
public function &__get($var) {
// I want to add a variable change listener here, that gets triggered
// when the references value has been changed.
// addListener(&$this->config[$var]['value'], array(&$this, 'changeListener'));
return $this->config[$var]['value'];
}
/*
public function changeListener(&$reference) {
// scan $this->cookies, find the variable that $reference is the reference to (don't know how to do that ether)
// send cookie
}
*/
public function __isset($var) {
return isset($this->cookies[$var]);
}
public function __unset($var) {
unset($this->cookies[$var]);
setcookie($this->cookieTag.'_'.$var,'',(time()-(172800)),'/','');
}
public function setCookieExpire($var, $value, $expire=null) {
if(!isset($expire)) {
$expire = $value;
$value = null;
}
if($expire<time()) $expire = time() + $expire;
if(isset($value)) $this->cookies[$var]['value'] = $value;
$this->cookies[$var]['expires'] = $expire;
setcookie($this->cookieTag.'_'.$var,json_encode((isset($value)?$value:(isset($this->cookies[$var]['value'])?$this->cookies[$var]['value']:''))),$expire,'/','');
}
}
至于为什么我不想有更新功能,它实际上只是个人偏好。这将在其他人可以扩展的框架中使用,并且我认为让他们能够将cookies作为单行代码中的变量来处理,从而变得更加轻松。
我不认为这是可能的在PHP中。对于实现SplObserver接口的自定义类可能是一个选项,但您需要在进行更改时通知。 – Orangepill
我不明白你的魔法获得方法与此有什么关系。另外,你的第二个代码示例似乎与你的第一个代码没有太大关系。我认为更好的代码示例会为我澄清事情。我只是抛出一些东西,希望它朝着正确的方向前进:__set()?!这样你可以看到变量何时变化。但是一旦我真的知道你想要什么,我将能够更好地帮助你。 PHP确实对Listener有一些功能。 –
@MichaSchwab我用示例类更新了我的问题。 – MrFigg