开发者

A thread/topic class in php

开发者 https://www.devze.com 2023-01-25 13:21 出处:网络
In college today we made a simple forum in procedural PHP. My homework is to make it so that it\'s in OOP (for comparison), this is where I\'m stuck.

In college today we made a simple forum in procedural PHP. My homework is to make it so that it's in OOP (for comparison), this is where I'm stuck.

In OOP a class should only do one thing, right? So a Topic class should allow a topic to be created, set whether replies are allow, and get its replies...

This is what I got so far,

<?php

abstract class Thread {

    protected $_name;   //thread name
    protected $_text;
    protected $_author;
    protected $_allowReplies = true;
    protected $_replies = array();


    function  __construct($name) 
    {
        $this->setName($name);
    }


    protected function setAuthor(User $author)
    {

    } //edited


    function setAllowReplies($replies)
    {
        if (is_bool($replies)) {
            $this->_allowReplies = $replies;
        }
        else
        {
            return false;
        }
    }

    function  setName($name)
    {
        $this->_name = $name;
    }

    function add开发者_JAVA技巧Reply($reply)
    {
        return $this_replies[] = $reply;
    }

    function  makeThread()      //builds up array of values to add to database
    {
        $values =  array();
        //add to database here
    }
}
?>

What I don't understand is, do I now have to create a class to add the values to the database and an abstract class to add Authors (using Type hinting .etc), or can I just do it here?

(The above class is incomplete because I got halfway and thought I was doing it wrong)

What I'm basically trying to say is, am I designing the above class correctly?


In OOP a class should only do one thing, right?

Not quite. A class gathers methods and properties that constitute an object. It is perfectly valid for an object of a particular class to be able to do more than just a single task.

What I don't understand is, do I now have to create a class to add the values to the database and an abstract class to add Authors (using Type hinting .etc), or can I just do it here?

You don't need an extra class to add a thread or its values to the database (you could, though). Simply offer interfaces (i.e. methods) to the world to add and manipulate a thread. Take the author as an example:

/*
 * Assigns the thread to the specified author
 * Returns boolean
 */
public function set_author(Author $a)
{
    // Forge you SQL query based on the author $a's data and
    // send it to your database

    // How this is done heavily depends on your table topology
}


First of all.. you could rename your class not to "Thread" is actually misleading. Now about your Class design, You'll first need to design your database schema. What is needed for storage? Relations? e.g.

Table Topic
(
    Topic_ID,
    Title_ID, 
    Author_ID, //Or User_ID
    Topic_Text,
    ....
)

Table Commends
(
    Comm_ID,
    Topic_ID, //Relation 1..n
    Title_ID,
    Author_ID,
    Comm_Text,
    ...
)

Then in your class schema, you could follow the "Strategy Pattern". in this case you build an abstract class called let's say DBObject ( DataBase's Object ), where every Object that has an instance in your Database is a DBObject ( Users, Topics, Commends etc ). A DBObject "has a" Database, so you also build a Database class that handles, executes and fetches every DB query. now you have your UML to start building your classes.

and an example:

$tpc = new Topic(); // initiates Topics variables, Begin Transaction
$tpc->setCategory( "..." ); //Sets the Category_ID
$tpc->setTitle( "..." ); //Create a Title in Title table, and Stores the Title_ID
$tpc->setAuthor( "User_ID" ); //Check if Author Exist, Sets the Auth_ID
$tpc->setText( $_POST['text'] ); //OMG Don't use the $_POST['text'] like this..!!
$tpc->execute(); //Sets DateTime Creation, etc. Executes Query, Ends Transaction.

Hope this'll help.. Enjoy..! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消