Niedawno rozpocząłem pisanie sobie zbioru przydatnych klas i funkcji, które roboczo nazywam moim mini 'frameworkiem'. Trafiła tam już m. in. klasa Uploader'a, którą pomogliście mi dopracować.
Stwierdziłem, że przy wielu projektach przydatna stanie się klasa wpisów inaczej newsów. Nowości występują w większości stron internetowych.
Przechodząc do sedna sprawy:
Myśląc abstrakcyjnie news posiada swoje prywatne pola jakimi są tytuł, treść, data itp. Jednak brakowało mi możliwości zarządzania wpisami tak bardziej 'z góry'.
Wymyśliłem klasę zarządzającą wpisami CNewsManager. Cała konstrukcja wydaje mi się o tyle korzystna (także pod względem wydajności), że do pobrania newsów wystarczy jedno zapytanie do bazy danych. Przejdźmy do kodu.
Klasa CNewsManager:
class CNewsManager { private $sTable; // Nazwa tabelki private $pdo; // Baza danych public function __construct($pdo, $sTable) { $this->sTable = $sTable; $this->pdo = $pdo; } /* Funkcja tworzą nowy wpis, zawartość rozszerzona pobierana jest opcjonalnie */ public function createNews($title, $content, $exContent = NULL) { if($title !== NULL && $content !== NULL) { if($exContent !== NULL) { $oNew = new CNews($this->pdo, $this->sTable, NULL, $title, $content, NULL, $exContent); $oNew->upload(); return $oNew; } else { $oNew = new CNews($this->pdo, $this->sTable, NULL, $title, $content, NULL); $oNew->upload(); return $oNew; } } else return false; } /* Funkcja ładująca wpisy do buffera newsów. Jeżeli nie podano id początkowego i końcowego, zapytanie pobiera wszystkie rekordy z tabelki. ZAPYTANIE DO BAZY! */ public function load($idStart = NULL, $idLimit = NULL) { if($idStart !== NULL && $idLimit !== NULL) { foreach($this->pdo->query('SELECT * FROM `'.$this->sTable.'` ORDER BY id DESC LIMIT '.$idStart.', '.$idLimit) as $row) { $this->aNewsBuffer[$row['id']] = $row; } return $this; } else { foreach($this->pdo->query('SELECT * FROM `'.$this->sTable.'`') as $row) { $this->aNewsBuffer[$row['id']] = $row; } return $this; } } /* Funkcja opróżniająca buffer newsów */ public function clearNewsBuffer() { { $this->aNewsBuffer[$i] = NULL; } return $this; } // Funkcja pobierająca obiekt newsa na podstawie podanego ID. Obiekt pobierany jest z bufora newsów. public function selectNews($nId) { { $aNews = $this->aNewsBuffer[$nId]; if($aNews['ex_tresc'] !== NULL) { $oNews = new CNews($this->pdo, $this->sTable, $aNews['id'] , $aNews['tytul'], $aNews['tresc'], $aNews['data'], $aNews['ex_tresc']); return $oNews; } else { $oNews = new CNews($this->pdo, $this->sTable, $aNews['id'] , $aNews['tytul'], $aNews['tresc'], $aNews['data']); return $oNews; } } else return false; } /* Wyrenderowanie wszystkich wpisów zawartych w bufferze */ public function showAll() { { if($this->aNewsBuffer[$i] !== NULL) { if($this->aNewsBuffer[$i]['ex_tresc'] !== NULL) { $oNews = new CNews($this->pdo, $this->sTable, $this->aNewsBuffer[$i]['id'], $this->aNewsBuffer[$i]['tytul'], $this->aNewsBuffer[$i]['tresc'], $this->aNewsBuffer[$i]['data'], $this->aNewsBuffer[$i]['ex_tresc']); $oNews->show(); } else { $oNews = new CNews($this->pdo, $this->sTable, $this->aNewsBuffer[$i]['id'], $this->aNewsBuffer[$i]['tytul'], $this->aNewsBuffer[$i]['tresc'], $this->aNewsBuffer[$i]['data']); $oNews->show(); } } } return $this; } }
Klasa CNews:
class CNews { private $id; // ID wpisu private $date; // Data private $title; // Tytuł private $content; // Zawartość podstawowa private $exContent; // Zawartość rozszerzona private $author; // Autor wpisu private $table; private $datebase; // Baza danych (PHP Data Objects) /* Konstruktor klasy wpisu. OPTYMALIZACJA! */ public function __construct($datebase, $sTable, $nId, $strTitle, $strContent, $date, $strExContent = NULL) { $this->datebase = $datebase; if ($strTitle !== NULL && $strContent !== NULL && $sTable !== NULL) { $nId === NULL ? $this->id = NULL : $this->id = $nId; $this->title = $strTitle; $this->content = $strContent; $this->table = $sTable; $strExContent === NULL ? $this->exContent = NULL : $this->exContent = $strExContent; /* Jeszcze autor */ return true; } else return false; } public function edit($strTitle, $strContent, $strExContent = NULL) { if($strTitle !== NULL && $strContent !== NULL) { $this->title = $strTitle; $this->content = $strContent; if($strExContent !== NULL) $this->exContent = $strExContent; } else return false; } /* Funkcja aktualizująca wpis w bazie danych. Zależnie od statusu news jest tworzony lub uaktualniany. ZAPYTANIE DO BAZY! */ public function upload() { if($this->title !== NULL && $this->content !== NULL && $this->table !== NULL) { if($this->id !== NULL) { $this->datebase->exec("UPDATE ".$this->table." SET tytul='".$this->title."', tresc='".$this->content."', ex_tresc='".$this->exContent."' WHERE id='".$this->id."'"); return true; } else { $this->datebase->exec("INSERT INTO ".$this->table." VALUES(0,'".$this->title."','".$this->content."', '".$this->exContent."', '".$this->date."', '".$this->author."')"); return true; } } else return false; } public function delete() { if($this->id !== NULL) $this->datebase->exec("DELETE FROM ".$this->table." WHERE id='".$this->id."'"); } /* Funkcja pobierająca tytuł */ public function getTitle() { return ($this->title !== NULL ? $this->title : false); } /* Funkcja pobierająca zawartość podstawową */ public function getContent() { return ($this->content !== NULL ? $this->content : false); } public function getExContent() { return ($this->exContent !== NULL ? $this->exContent : false); } public function show() { echo '<tr> <td><h2>'. $this->title .'</h2> <span>'. $this->date .'</span> </td> </tr>'; if($this->exContent === NULL) echo ' <tr> <td colspan="2"><div class="wiadomosc">'. $this->content .'</div></td> </tr>'; else echo '<tr> <td colspan="2"><div class="wiadomosc">'. $this->content .'<div style=" float: right; margin-right: 35px; margin-top: 15px;"><a href="?home=home&more='.$this->id.'">Czytaj więcej...</a></div></div></td> </tr>'; } public function show_ex() { echo '<tr> <td><h2>'. $this->title .'</h2> <span>'. $this->date .'</span> </td> </tr> <tr> <td colspan="2"><div class="wiadomosc">'. $this->exContent .'</div></td> </tr>'; } }
Tutaj pojawia się moje pytanie: czy koniecznością jest przekazywanie takiej ilości parametrów do konstruktora CNews?
Chciałbym także poznać wasze opinie co do budowy i mojego sposobu myślenia w tym przypadku. Chętnie dowiem się jakie rozwiązania proponujecie oraz jakie porady macie dla mnie co do pisania kodu.
Teraz skrawek kodu przykładowego:
$pdo = new PDO('mysql:host=localhost;dbname=newsy', 'root', ''); $NewsManager = new CNewsManager($pdo, 'news'); $NewsManager->load(1,11); $NewsManager->selectNews(5)->show();
Pozdrawiam
Jakieś propozycje?
PS. Nie wiem czy temat umieściłem w odpowiednim dziale, w razie problemów proszę moderatora o przeniesienie.
Już niedługo dodam także klasę komentarzy.
Pozdrawiam