나는이 질문을 오랫동안 궁금해합니다. PHP에서 참조를 처리하는 것이 좋은 생각이며 사용법을 설명하는 것보다 더 잘 설명 할 수 없으므로 다음 클래스를 살펴보고 @ setResult 메소드의 주석.PHP 메모리 참조
우리는 모델 뷰 컨트롤러 프레임 워크를 사용하고 있고 기본 AjaxController를 구축하고 있다고 상상해보십시오. 지금까지는 하나의 액션 메소드 (getUsers) 만있었습니다. 의견을 읽고, 내 질문에 분명히 희망, PHP는 이런 종류의 상황을 처리하고 메모리에 x 번에 대해 쓴 x 사실 setResult docblock.
class AjaxController{
private $json = array(
'result' => array(),
'errors' => array(),
'debug' => array()
);
/**
* Adds an error, always displayed to users if any errors.
*
* @param type $description
*/
private function addError($description){
$this->json['errors'][] = $description;
}
/**
* Adds an debug message, these are displayed only with DEBUG_MODE.
*
* @param type $description
*/
private function addDebug($description){
$this->json['debug'][] = $description;
}
/**
* QUESTION: How does this go in memory? Cause if I use no references,
* the array would be 3 times in the memory, if the array is big (5000+)
* its pretty much a waste of resources.
*
* 1st time in memory @ model result.
* 2th time in memory @ setResult ($resultSet variable)
* 3th time in memory @ $this->json
*
* @param array $resultSet
*/
private function setResult($resultSet){
$this->json['result'] = $resultSet;
}
/**
* Gets all the users
*/
public function _getUsers(){
$users = new Users();
$this->setResult($users->getUsers());
}
public function __construct(){
if(!DEBUG_MODE && count($this->json['debug']) > 0){
unset($this->json['debug']);
}
if(count($this->json['errors']) > 0){
unset($this->json['errors']);
}
echo json_encode($this->json);
}
}
또 다른 간단한 예 : 기술 A를 사용하는 것이 좋을 것이다 무엇 :
function example(){
$latestRequest = $_SESSION['abc']['test']['abc'];
if($latestRequest === null){
$_SESSION['abc']['test']['abc'] = 'test';
}
}
또는 기술 B :
function example(){
$latestRequest =& $_SESSION['abc']['test']['abc'];
if($latestRequest === null){
$latestRequest = 'test';
}
}
읽어 주셔서 감사하고 조언 :
참고 문헌에 대한 PHP 매뉴얼 페이지 : [http://php.net/manual/en/language.references.php](http://php://php.net/manual/en/language.references.php) – bfavaretto