我很長一段時間想知道這個問題,PHP如何處理引用是他們的一個好主意,我不能解釋比使用示例更好,讓我們看看下面的類,然後@ setResult方法的註釋。讓我們想象我們正在使用模型視圖控制器框架,並且我們正在構建基本的AjaxController,到目前爲止我們只有1個操作方法(getUsers)。閱讀評論,我希望我的問題很清楚,PHP如何處理這種情況,並且是我在內存@ setResult docblock中寫了x次的真實情況。PHP內存引用
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);
}
}
另一個簡單的例子:什麼是更好地使用技術答:
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.net/manual/en/language.references.php) – bfavaretto 2012-02-13 19:20:14