www.webdeveloper.com
Results 1 to 8 of 8

Thread: valid use for a class?

  1. #1
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654

    valid use for a class?

    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());


  2. #2
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    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.
    Attached Files Attached Files

  3. #3
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    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!

  4. #4
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    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.

  5. #5
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    Oops, new is a language construct (used to create a new instance of an object). Try renaming it, create maybe?

  6. #6
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Used add() and it's fine now. Just cleaning up the horrendous SQL statemenets now.
    Thanks again

  7. #7
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    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.

  8. #8
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    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();
    }

    // catch exceptions
    catch (Exception $e)
    {
        echo 
    "Oops... " $e->getMessage();


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles