I have 3 functions for use with posts on a blog.
I am wondering if this would be a valid reason for using a class?
I 'm not at this tsage looking for someone to show me what the class would look like. That I can look into once I know the answer to this question. All I really need to know is if this would be a good reason to use a class (sorry I've still not got my head around when I should use a class regardless of all of the posts here and all of the tutorials I have read.)
P.s. This code has not been properly tested yet. It's probably ridden with errors. I'll sort it out once I get to that stage.
Cheers
PHP Code:
function newPost($post_parent, $post_type, $post_user, $post_title, $post_body){
connectDB();
$query = "INSERT '', $post_parent, $post_type, $post_user, $post_title, $post_body, NOW(), '0' INTO a_post";
$result = mysql_query($query) or die('Not posted: ' . mysql_error());
}
function editPost($new_title, $new_body){
connectDB();
$query = "UPDATE a_post SET post_title = $new_title, post_body = $new_body WHERE post_id = $post_id";
$result = mysql_query($query) or die('Post not edited: ' . mysql_error());
}
function deletePost($post_id){
connectDB();
$query = "UPDATE a_post SET post_status = 1 WHERE post_id = $post_id";
$result = mysql_query($query) or die('Post not deleted: ' . mysql_error());
}
Sure, I've attached a full-scale solution so if youd rather do it all yourself you can without looking at any source-code. But yes, the above functions could definetly be migrated into a single Post class.
Thanks. I think I'm finally beginning to understand what's happening with classes.
Unfortunately my server is still PHP 4 (emailed the host earlier on today to see if they had plans to upgrade and was advised "hopefully in a couple of months but no exaact time frame")
I tried my own class then compaired it with yours' and (apart from the PHP5 parts) was pretty much identical. Thanks again!
is new() already a function in PHP? when I try to use the class it throws up an error but when I change the function name to Xnew() it's fine.
I tried to look for new on php.net but couldn't find anything on it.
personally, for PHP to be truly OO, it needs to have packages and security. while pcthug's code is perfectly valid, most other languages use getters/setters to retrieve and set values in the object.
Ok, here is the class definition with the addition of overloading getters/setters (though for now you will only be using setters) and protected property values (Not visible to the current script, only child objects). Now, access may be restricted within the getters/setters - thus making the object more secure (as chazzy mentioned previously).
PHP Code:
/*
Assuming PHP5 Environment
*/
class Post
{
protected $parent;
protected $title;
protected $body;
protected $user;
protected $type;
protected $id;
final public function __construct()
{
// db connection should be handled within a db class though
//connectDB();
}
final public function __set($property, $value)
{
if (property_exists(__CLASS__, $property))
{
$this->$property = $value;
return true;
}
// FATAL ERROR: attempt to set undefined property
return false;
}
final public function __get($property)
{
if (property_exists(__CLASS__, $property))
{
return $this->$property;
}
// FATAL ERROR: call to undefined property
return false;
}
public function add()
{
// again. db activity should be handled with a seperate db class
$query = "INSERT '', $this->parent, $this->type, $this->user, $this->title, $this->body, NOW(), '0' INTO a_post";
// exceptions should be used over errors in general oop design
if (!mysql_query($query))
{
throw new Exception('Not posted: ' . mysql_error());
}
}
public function edit()
{
$query = "UPDATE a_post SET post_title = '$this->title', post_body = '$this->body' WHERE post_id = '$this->id'";
if (!mysql_query($query))
{
throw new Exception('Post not edited: ' . mysql_error());
}
}
public function delete()
{
$query = "UPDATE a_post SET post_status = 1 WHERE post_id = $this->id";
if (!mysql_query($query))
{
throw new Exception('Post not deleted: ' . mysql_error());
}
}
}
// try some example post situations
try
{
// new post example
$post = new Post;
$post->parent = 'Parent Value';
$post->type = 5;
$post->user = 'Bob';
$post->title = 'My First Post.';
$post->body = 'Lorem Ipsum...';
$post->add();
// edit post example
$post = new Post;
$post->title = 'Hello, World!';
$post->body = 'Edited Body.';
$post->id = 69;
$post->edit();
// delete post example
$post = new Post;
$post->id = 1;
$post->delete();
}
Bookmarks