ArrayAccess
PHP 인터페이스에 대한 지난 질문을 많이 읽었으며 참조를 반환 할 수있는 방법은 offsetGet
입니다. 이 클래스는 array
변수를 래핑하는 간단한 클래스를 구현했습니다. offsetGet
메서드는 참조를 반환하지만 Only variable references should be returned by reference
이라는 오류가 발생합니다. 왜?PHP ArrayAccess - 참조가있는 다차원 배열 및 offsetGet
class My_Class implements ArrayAccess {
private $data = array();
...
public function &offsetGet($offset) {
return isset($this->data[ $offset ]) ? $this->data[ $offset ] : null;
}
...
}
나는이 클래스와 다차원 배열을 사용할 수 있도록하고 싶습니다
:
$myclass = new My_Class();
$myclass['test'] = array();
$myclass['test']['test2'] = array();
$myclass['test']['test2'][] = 'my string';
변수에 언 바운드 된'NULL '을 반환하기 때문에, 변수를'if (! thiset> data [$ offset]) $ this-> data [$ offset] = null; return $ this-> data [$ offset];'?/테스트하기에는 너무 느림 – Wrikken
함수가 참조가 아닌 표현식의 결과를 반환하고 '& null'참조를 사용할 수 없습니다. 임시 변수를 사용해보십시오 – mario