2011-09-28 3 views
3

가능한 중복 : 다음 두 예제의 차이는 (있는 경우), 그리고 무슨 일이 무엇
Initializing Class Fields at the Field Definition or in Class Contructor
Setting variables on Constructor VS on the class definition초기화 객체 속성 - 그것을 어떻게 제대로

개체 속성을 초기화하는 적절한 방법? 이 경우 $ fields의 내용은 미리 정의 된 것이므로 런타임에 변경되지 않습니다.

예 1 :

class User 
{ 
    $fields = array('username', 'password', 'email'); 

    function __construct() 
    { 
    }  
} 

예 2 :

class User { 
    $fields; 

    function __construct() 
    { 
     $this->fields = array('username', 'password', 'email'); 
    }  
} 
+0

다른 언어 ... – jondavidjohn

+0

@jondavidjohn : 수정 됨. – hakre

+0

@Brad 예 물론 이것은 단지 예일뿐입니다;) –

답변

1

첫 번째 예 ... 그것은 $fields의 의도가 무엇인지 독자에게 더 명확하고 분명하다 ... "뭔가 미리 정의 됨 "