问题描述
首先,从OLE'手册上:
First, a quote from the ole' manual on ArrayAccess::offsetSet()
:
此功能不通过引用赋值,并与的(在这个意义上间接它们由不通过直接改变尺寸,但通过改变子尺寸或子属性或参照另一个变量分配阵列的尺寸)。 相反,被调用。该操作只能成功,如果参照该方法的回报,这是唯一可能的,因为PHP 5.3.4
我有点这个混淆。看来,这表明(为5.3.4 的)可以定义 offsetGet()
通过引用实现类返回,因此处理通过引用赋值。
I'm a bit confused by this. It appears that this suggests that (as of 5.3.4) one can define offsetGet()
to return by reference in an implementing class, thus handling assignments by reference.
所以,现在的测试片段:
So, now a test snippet:
(忽略缺乏验证和使用isset()
检查的)
class Test implements ArrayAccess
{
protected $data = array();
public function &offsetGet($key)
{
return $this->data[$key];
}
public function offsetSet($key, $value)
{
$this->data[$key] = $value;
}
public function offsetExists($key) { /* ... */ }
public function offsetUnset($key) { /* ... */ }
}
$test = new Test();
$test['foo'] = 'bar';
$test['foo'] = &$bar; // Fatal error: Cannot assign by reference to
// overloaded object in
var_dump($test, $bar);
好了,不工作。那么,这是什么手册注是指什么?
Ok, so that doesn't work. Then what does this manual note refer to?
原因结果
我想引用通过数组操作者允许分配实施 ArrayAccess接口
的对象,作为例子的代码片段显示。我以前研究过这一点,我不认为这是可能的,不过话说回来,由于这个不确定性,我(的重 - 的)发现了这个提及手册中的现在,我只是困惑。
更新:当我打张贴您的问题骨节病>,我意识到,这很可能只是通过引用的另一个变量,如提及转让为 $栏=安培; $测试['富'];
。如果是这样的话,那么道歉;但不知如何,如果它是在所有可能,通过参考重载对象分配将是巨大的。
Update: As I hit , I realized that this is likely just referring to assignment by reference to another variable, such as $bar = &$test['foo'];
. If that's the case, then apologies; though knowing how, if it is at all possible, to assign by reference to the overloaded object would be great.
进一步阐述:什么这一切归结为是我想有以下方法别名:
Further elaboration: What it all comes down to, is I would like to have the following method aliases:
isset($obj[$key]); // $obj->has_data($key);
$value = $obj[$key]; // $obj->get_data($key);
$obj[$key] = $value; // $obj->set_data($key, $value);
$obj[$key] = &$variable; // $obj->bind_data($key, $variable);
// also, flipping the operands is a syntactic alternative
$variable = &$obj[$key]; // $obj->bind_data($key, $variable);
unset($obj[$key]); // $obj->remove_data($key);
至于的
, GET
,设置
和删除
去,他们用 ArrayAccess接口
的支持的方法没有问题。绑定功能是我在哪里茫然了,现在开始接受了ArrayAccess和PHP的局限性,仅仅是望而却步了这一点。
As far as has
, get
, set
, and remove
go, they're no problem with the supported methods of ArrayAccess
. The binding functionality is where I'm at a loss, and am beginning to accept that the limitations of ArrayAccess and PHP are simply prohibitive of this.
推荐答案
这不符合 ArrayAccess接口
工作,你可以自己添加一个公共功能,允许您设置参考偏移(当然,这看起来使用数组语法不同,所以它不是一个真正的足够的答案):
This does not work with ArrayAccess
, you could add yourself a public function that allows you to set a reference to an offset (sure, this looks different to using array syntax, so it's not really a sufficient answer):
class Test implements ArrayAccess{
protected $_data = array();
public function &offsetGet($key){
return $this->_data[$key];
}
...
public function offsetSetReference($key, &$value)
{
$this->_data[$key] = &$value;
}
}
$test = new Test();
$test['foo'] = $var = 'bar';
$test->offsetSetReference('bar', $var);
$var = 'foo';
echo $test['bar']; # foo
$alias = &$test['bar'];
$alias = 'hello :)';
echo $var; # hello :)
在 ArrayAccess接口
首次实现可能这样的功能被遗忘了。
Probably such a function was forgotten when ArrayAccess
was first implemented.
编辑:将它作为参考分配:
class ArrayAccessReferenceAssignment
{
private $reference;
public function __construct(&$reference)
{
$this->reference = &$reference;
}
public function &getReference()
{
$reference = &$this->reference;
return $reference;
}
}
class Test implements ArrayAccess{
...
public function offsetSet($key, $value){
if ($value instanceof ArrayAccessReferenceAssignment)
{
$this->offsetSetReference($key, $value->getReference());
}
else
{
$this->_data[$key] = $value;
}
}
然后完美的作品,因为你实现它。这可能比上述更明确的 offsetSetReference
种更漂亮的接口:
$test = new Test();
$test['foo'] = $var = 'bar';
$test['bar'] = new ArrayAccessReferenceAssignment($var);
$var = 'foo';
echo $test['bar']; # foo
$alias = &$test['bar'];
$alias = 'hello :)';
echo $var; # hello :)
这篇关于在ArrayAccess接口PHP - 分配参照抵消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!