开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜