Hallo allemaal,
Na het lezen van de tut van Blache wilde ik een kijken of mijn eigen OOP kunsten een beetje in orde zijn.
Nu heb ik deze 2 classes:
<?php
/**
* CLASS
*/
class Page {
private $_id;
private $_title;
private $_content;
private $_parentId;
private $_isHome;
public function __construct() {
$this->_id = 0;
$this->_title = '';
$this->_content = '';
$this->_parentId = null;
$this->_isHome = 0;
}
public function setId($id) {
if(ctype_digit($id)) {
$this->_id = $id;
} else {
throw new Exception('Id is not a digit.');
}
}
public function setTitle($title) {
if(ctype_alnum($title)) {
$this->_title = $title;
} else {
throw new Exception('Title is not alphanumaric.');
}
}
public function setContent($content) {
$this->_content = $content;
}
public function setParentId($parentId) {
if(ctype_digit($parentId) || $parentId === null) {
$this->_parentId = $parentId;
} else {
throw new Exception('ParentId is neither a digit nor NULL.');
}
}
public function setIsHome($isHome) {
if($isHome == 0 || $isHome == 1) {
$this->_isHome = $isHome;
} else {
throw new Exception('IsHome is not 0 or 1.');
}
}
public function getId() {
return $this->_id;
}
public function getTitle() {
return $this->_title;
}
public function getContent() {
return $this->_content;
}
public function getParentId() {
return $this->_parentId;
}
public function getIsHome() {
return $this->_isHome;
}
}
class Page_Controller {
private function $_page;
public function __construct(Page $page) {
$this->_page = $page;
}
public function new() {
mysql_query("
INSERT INTO
pages
(
title,
content,
parent_id,
is_home
)
VALUES
(
'" . $this->_page->getTitle() . "',
'" . mysql_real_escape_string($this->_page->getContent()) . "',
'" . $this->_page->getParentId() . "',
'" . $this->_page->getIsHome() . "'
)
");
}
public function edit() {
mysql_query("
UPDATE
pages
SET
title = '" . $this->_page->getTitle() . "',
content = '" . mysql_real_escape_string($this->_page->getContent()) . "',
parent_id = '" . $this->_page->getParentId() . "',
is_home = '" . $this->_page->getIsHome() . "'
WHERE
id = '" . $this->_page->getId() . "'
");
}
public function delete() {
mysql_query("DELETE FROM pages WHERE id = '" . $this->_page->getId() . "'");
}
public function getData() {
$result = mysql_query("SELECT * FROM pages WHERE id = '" . $this->_page->getId() . "'");
return mysql_fetch_assoc($result);
}
}
/**
* USAGE
*/
try {
/* create a page */
$page = new Page();
$page->setTitle('Sub pagina');
$page->setContent('Lorum ipsum..');
$page->setParentId(1);
$page->setIsHome(0);
$pageController = new Page_Controller($page);
$pageController->new();
/* edit a page */
$page = new Page();
$page->setId(1);
$page->setTitle('Hoofd pagina');
$page->setContent('Lipsum lorum pipsum..');
$page->setParentId(null);
$page->setIsHome(1);
$pageController = new Page_Controller($page);
$pageController->edit();
/* delete a page */
$page = new Page();
$page->setId(1);
$pageController = new Page_Controller($page);
$pageController->delete();
/* get data of a page */
$page = new Page();
$page->setId(1);
$pageController = new Page_Controller($page);
print_r($pageController->getData());
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
}
?>
Ik heb er voor gekozen om verschillende "set" functies te maken, en niet alles via de constructor mee te geven.
Dit heb ik gedaan omdat je bij het verwijderen van een pagina alleen het id nodig hebt, bij het bewerken echter ook de titel, inhoud etc..
Ik hoor graag jullie mening.
Groetjes,
Boris
ps: ik zou idd een PDO achtig iets moeten gebruiken voor m'n database werk, maar dit heb ik net ff snel in elkaar geflanst had geen zin om dat uit te gaan zoeken. Misschien dat ik dat na jullie reviews nog ff doe.. ;)
3.616 views