开发者

Initializing object properties - how to do it properly [duplicate]

开发者 https://www.devze.com 2023-04-08 02:40 出处:网络
This question already has answers 开发者_Python百科here: Closed 11 years ago. Possible Duplicate: Initializing Class Fields at the Field Definition or in Class Contructor
This question already has answers 开发者_Python百科here: Closed 11 years ago.

Possible Duplicate:

Initializing Class Fields at the Field Definition or in Class Contructor

Setting variables on Constructor VS on the class definition

What is the difference (if any) between the following 2 examples, and what is the proper way to initialize object properties? Please note that the content of $fields in this case is something predefined, it will not change in runtime.

Example 1:

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

    function __construct()
    {
    }    
}

Example 2:

class User {
    $fields;

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


The first example... It is more explicit and obvious to the reader what the intention of $fields is... "something predefined"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号