2017-10-08 5 views
0

$this->user->settings의 복제본을 $this->updateUser에 전달하여 원래 배열에 영향을 미치지 않습니다.PHP - 객체 복제

private function updateSettings($field, $value) 
{ 
    echo gettype($this->user->settings); // array 
    $this->user->settings[$field] = $value; 
    $this->updateUser('settings', json_encode($this->user->settings)); 
    echo gettype($this->user->settings); // string 
} 

어떻게 수행 할 수 있습니까?

+0

개체 복제가 필요한 경우 clone 키워드를 사용하여 개체 복제 –

+0

키워드 clone은 어떤 이유로 스크립트를 중지합니다. –

답변

0

당신은 단순히 다음과 같은 작업을 수행 할 수

private function updateSettings($field, $value) 
{ 
    $newArray = array_merge($this->user->settings, [$field => $value]); 

    $this->updateUser('settings', json_encode($newArray)); 
} 

, BTW object cloning 다른 일이이 경우에 해당하지 않습니다.