哪種方法更好的做法?
$user = new User();
$user->username = "bob";
$user->password = "password";
$user->age = 42;
echo $user->username;
與
$user = new User();
$user->setUsername("bob")->setPassword("password")->setAge(42);
echo $user->getUsername();
是更好的做法,以便訪問和方法來設置對象變量或直接? 用戶是用戶對象模型。提前致謝。
神奇的方法是不正確的getter和setter的替代品,將它們用於這個目的比使用常規功能顯著慢。我們也有很多人相信使用受保護的而不是私有的默認可見度。 – Gordon 2010-11-03 09:17:44
如何設置默認的可見性? – axsuul 2010-11-03 09:38:27
@Axsuul對不起,如果這是unlear:我的意思與*默認可見*是寫你的類時要使用的關鍵字。正如Stegemann正確指出的那樣,內部默認可視性不能改變。除非你聲明不同,否則它將被公開。 – Gordon 2010-11-03 10:12:20