我有两个简单的问题,但找不到正确的答案。我必须如何定义我的属性?我可以在一行中定义吗?两者都有效,但什么是正确的?
如何定义具有相同值的乘法属性?
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();
对于"快速而肮脏"的东西,只要以后能理解,每行使用多个定义是可以的。
对于更正式的代码,您应该每行定义一个,特别是如果您计划在某个时候编写PHPDoc,或者其他人正在阅读它
话虽如此,习惯于一直以"正确"的方式做这件事总是一种很好的做法。