这是一个场景
class page {
public $name;
public $title;
public function showhead() {
return "<head><title>".$this->title."</title></head>";
}
}
$mypage = new page;
$mypage->title = "My Page title";
$mypage->showhead();
另一种情况
class page {
public $name;
public function showhead($title) {
return "<head><title>".$title."</title></head>";
}
}
$mypage = new page;
$mypage->showhead("My Page title");
在这些方法中,哪种更好,哪种应该避免?为什么?
最佳答案
我想这取决于您是否再次需要该头衔。如果您这样做,则为存储和检索创建属性。如果您只需要一次,则使用方法参数。
关于php - 在 OOP 中应该避免并尽可能多地使用在方法中传递参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3174096/