我有两个简单的问题,但找不到正确且正确的答案。我该如何定义我的属性?我可以在一行中定义吗?两者都有效,但是什么是正确的?
如何使用相同的值定义乘法属性?
class Page {
private $value;
public $title, $footer, $content; // like this?
// or like this?
public $title;
public $footer
public $content;
// multiply one line with the same value?
public $name, $age, $place = "none";
public $name = $age = $place = "none"; // error
public function __construct($value) {
$this->value = $value;
}
private function Header() {
$q = "<!DOCTYPE html>\n";
$q .= "<html>\n";
$q .= "<head><title>".$this->title."</title></head>\n";
$q .= "<body>\n\n";
return $q;
}
private function Footer() {
$q = "\n\n<div class=\"footer\">".$this->footer."</div>";
$q .= "\n</body>\n</html>";
return $q;
}
private function Content() {
return $this->content;
}
public function DrawPage() {
echo $this->Header();
echo $this->Content();
echo $this->Footer();
}
// for display only content in a jQuery ajax post
public function DrawContent() {
echo $this->Content();
}
}
$page = new Page('/clean/url/item/5/page-title.html');
$page->header = "my test page";
$page->footer = "this is the footer text";
$page->content = "Lorem Ipsum is simply dummy text.";
$page->DrawPage();
答案 0 :(得分:2)
对于“快速和肮脏”的东西,只要你以后可以理解它,每行使用多个定义就可以了。
对于更正式的代码,您应该每行定义一个,特别是如果您计划在某个时间点写PHPDoc或者其他人正在阅读它。
话虽如此,但始终习惯于以“适当”的方式做到这一点总是好的做法。