Hi,
I would like to build myself a OOP based guestbook (with admin section if that's possible) to reuse easy in my websites... (I was thinking about using OOP to make the reuse of my guestbook easier).
In this way I want to create insight on how to program correctly in OOP in PHP.
Anyone care to help me on this? :)
Building a OOP based guestbook
Started by Coldhearth, Dec 08 2008 09:36 AM
2 replies to this topic
#1
Posted 08 December 2008 - 09:36 AM
|
|
|
#2
Posted 08 December 2008 - 09:45 AM
By now I have written a class called Post (Post.class.php file in model folder)
My structure is:
model
Post.class.php
view
index.php
controller
Here's the Post class that represents a post in the guestbook.
My structure is:
model
Post.class.php
view
index.php
controller
Here's the Post class that represents a post in the guestbook.
<?php
/*
* Een post from a guestbook has an:
* - Title
* - Content
* - Date
* - Emailadres
*/
class Post{
//Post attributes
private $id;
private $title;
private $date;
private $email;
private $content;
//Post constructor
public function __construct($id="", $title="", $date="", $email="", $content=""){
$this->setId($id);
$this->setTitle($title);
$this->setDate($date);
$this->setEmail($email);
$this->setContent($content);
}
//Post setters
public function setId($id){
if(is_integer($id)){
$this->id = $id;
}else{
throw new Exception("Je hebt geen geldige id opgegeven");
}
}
public function setTitle($title){
if(strlen($title)>0){
$this->title = $title;
}else{
throw new Exception("Je moet een titel invullen");
}
}
public function setDate($date){
$this->date = $date;
}
public function setEmail($email){
$this->email = $email;
}
public function setContent($content){
if(strlen($content)>3){
$this->content = $content;
}else{
throw new Exception("Je bericht is te kort.");
}
}
//Post getters
public function getTitle(){
return $this->title;
}
public function getDate(){
return $this->date;
}
public function getEmail(){
return $this->email;
}
public function getContent(){
return $this->content;
}
}
?>
#3
Posted 08 December 2008 - 10:14 AM
I've added this class Post_store with a funciton to get all the posts from the guestbook (also in de model folder)
Here it is:
Here it is:
<?php
class Post_store{
public function __construct(){
mysql_connect("localhost", "root", "root") or die("Kon niet verbinden met de server");
mysql_select_db("guestbook") or die("Kon niet verbinden met de database.");
}
//This function returns an array of all the posts.
public function showAll(){
$posts = array();
$result = mysql_query("SELECT * FROM guestbook") or die("Fout in ophaal query");
if(mysql_num_rows($result)>0){
while($row = mysql_fetch_array($result)){
foreach($row as $post_data){
$posts[] = new Post($post_data[0], $post_data[1], $post_data[2], $post_data[3], $post_data[4]);
}
}
}
return $posts;
}
}
?>


Sign In
Create Account


Back to top









