我在以面向对象的方式使用PHP方面相对较新。在我的最新项目中,我将创建一个网站,从SQL数据库中加载大量条目,然后将这些条目发送到视图进行输出。
在研究如何以最佳方式完成此操作时,我已编写了以下代码:
class entryHandler {
//Store database connection
protected $database;
//List of all entries as multidimensional array fetched from SQL.
private $entriesSQL = array();
//List of all entries (as objects) will be stored in our $entries array
private $entriesObjects = array();
/*
*
* Upon construct
*
*/
public function __construct($database) {
//Check if database class has been passed correctly.
//Else we exit, since we can not function without it!
if (isset($database)) {
$this -> database = $database;
//Get events
if ($entriesSQL = $this -> getEventsFromSQL()) {
// Set SQL entries
$this -> setEntriesSQL($entriesSQL);
//And turn them into an array of objects
$this -> setEntryObjects();
}
else {
}
}
else {
echo 'No database connection has been established. Exiting execution.';
exit();
}
}
/*
* Setter for entriesSQL
*/
private function setEntriesSQL($entriesSQL) {
$this -> entriesSQL = $entriesSQL;
}
/*
* Getter for entriesSQL
*/
public function getEntriesSQL() {
return $this -> entriesSQL;
}
/*
* Setter for entry objects
*
*/
private function setEntryObjects() {
foreach ($this -> getEntriesSQL() as $key => $val) {
//entryID, entryUserID, entryTitle, entryTimeStart, entryTimeEnd
array_push($this -> entriesObjects, new entry($val['entryID'], $val['entryUserID'], $val['entryTitle'], $val['entryTimeStart'], $val['entryTimeEnd']));
}
}
/*
*
* Getter for entry objects
*
*/
public function getEntryObjects() {
return $this -> entriesObjects;
}
/*
*
* This function grabs events from SQL and return multidimensional-array of content.
*
*/
private function getEventsFromSQL() {
//Define the SQL query
$SQL = '...snippet...';
if ($entriesSQL = $this -> database -> read($SQL)) {
//Entries collected, return them.
return $entriesSQL;
}
else {
//Error occured. Log it and return false.
return false;
}
}
}
class entry {
var $entryID;
var $entryUserID;
var $entryTitle;
var $entryTimeStart;
var $entryTimeEnd;
public function __construct($entryID, $entryUserID, $entryTitle, $entryTimeStart, $entryTimeEnd) {
$this -> setEntryID($entryID);
$this -> setEntryUserID($entryUserID);
$this -> setEntryTitle($entryTitle);
$this -> setEntryTimeStart($entryTimeStart);
$this -> setEntryTimeEnd($entryTimeEnd);
}
/*
* Setter for entryID
*/
private function setEntryID($entryID) {
$this -> entryID = $entryID;
}
/*
* Getter for entryID
*/
public function getEntryID() {
return $this -> entryID;
}
/*
* Setter for entryUserID
*/
private function setEntryUserID($entryUserID) {
$this -> entryUserID = $entryUserID;
}
/*
* Getter for entryUserID
*/
public function getEntryUserID() {
return $this -> entryUserID;
}
/*
* Setter for entryTitle
*/
private function setEntryTitle($entryTitle) {
$this -> entryTitle = $entryTitle;
}
/*
* Getter for entryTitle
*/
public function getEntryTitle() {
return $this -> entryTitle;
}
/*
* Setter for entryTimeStart
*/
private function setEntryTimeStart($entryTimeStart) {
$this -> entryTimeStart = $entryTimeStart;
}
/*
* Getter for entryTimeStart
*/
public function getEntryTimeStart() {
return $this -> entryTimeStart;
}
/*
* Setter for entryTimeEnd
*/
private function setEntryTimeEnd($entryTimeEnd) {
$this -> entryTimeEnd = $entryTimeEnd;
}
/*
* Getter for entryTimeEnd
*/
public function getEntryTimeEnd() {
return $this -> entryTimeEnd;
}
}
现在,类“entryHandler”是从我的控制器(另一个PHP文件)加载的,如下所示:
//Instantiate calenderHandler and pass our database handler to it.
$entryHandler = new entryHandler($database);
//Load template
$template -> calendarEntries = $this -> renderEntries($entryHandler -> getEntryObjects());
/*
*
* This functions renders entries content for view.
* Returns string with content.
*
*/
private function renderEntries($entries) {
$entriesContent = '';
foreach ($entries as $entry) {
$entriesContent .= '{
title: "' . $entry -> getEntryTitle() . '",
start: "' . $entry -> getEntryTimeStart() . '",
end: "' . $entry -> getEntryTimeEnd() . '"
}, ';
}
//Right trim entries string, so that we do not add trailing comma to last entry in list.
$entriesContent = rtrim($entriesContent, ', ');
//And return content to parse it to view.
return $entriesContent;
}
}
这是一个不好的设计模式吗?有没有其他方法可以做到?在这段代码中是否存在某种冗余?-需要注意的事情?
感谢大家抽出时间。
问候, fischer
@param string $arg @return int|bool
。 - Yang->
)两侧加空格也不是错误的,但我个人认为看起来很奇怪。最后,在你有一个private
方法或属性时,决定是否真的想让它对子类不可访问 - 这些可以是protected
,从而通过继承可用。 - halfer