2010-11-18 7 views
1

다른 클래스에서 참조해야하는 $ 데이터베이스 객체가 있습니다.
이것이 올바른 방법일까요?클래스에 대한 참조 전달

<?php 
class User { 
    private $database; 

    function getPosts($limit = 1) { 
    return $this->database->query("select..."); 
    } 

    function __construct(&$database) { 
    $this->database = $database; // Do I need an another ampersand here? 
    } 
} 

$user = new User($database); // $database is defined in an earlier include 
?> 

답변

3

모양에서 보면 앰퍼샌드가 필요 없습니다. 얼마나 많은 참조가 있더라도 모두 동일한 $database 개체를 참조해야합니다.

function __construct($database) { 
    $this->database = $database; 
    } 
+0

멋지다. 나는 확실하지 않았다. 감사 – arby

관련 문제