-1

嘿,我是 PHP 中的 OO 新手,但我了解基本的 Java 并了解 Java 中的 OO,但我试图了解如何通过 OO 使其与 HTML 正确交织。为什么下面的代码不会吐回消息?谢谢。

<?php

class SubmitPost {

    public function __construct() {
        $Db = new PDO('mysql:host=localhost;dbname=Comment', 'root', '');
    }

    public function Comment($Post, $Time, $Title) {

        $Post = strip_tags($_POST['Post']);
        $Time = time();
        $Title = strip_tags($_POST['Title']);

            $Messages = array('success' => 'Your comment has been added.', 'error' => 'There was a problem adding your comment.');

                if(isset($Post, $Title)) {
                    return $Messages['success'];
                } else {
                    return $Messages['error'];
                }

    }
}

$New = new SubmitPost;
var_dump($New);
?>

<html>
    <head>
    </head>

    <body>
        <form action="OO.php" method="POST">
            <input type="text" name="Title" placeholder="Your Title"><br />
            <textarea placeholder="Your Comment" name="Post"></textarea><br />
            <input type="submit" value="Comment">
        </form>
    </body>
</html>
4

2 回答 2

1

您需要在某处调用您的方法。

$New = new SubmitPost();
echo $New->Comment("needless","because","unused");  // You are not using these values in your method
//var_dump($New);

编辑 这不是真正的OOP。它应该是这样的

public function Comment($Post, $Time, $Title) {
    $Post = strip_tags($Post);
    $Title = strip_tags($Title);
   //....
   }

并称它为

$New = new SubmitPost();
echo $New->Comment($_POST["Post"],time(),$_POST["Title"]); 
于 2013-09-27T04:58:54.640 回答
0

您的对象中没有任何内容。这就是为什么你没有得到任何输出。试试下面的代码:

<?php

class SubmitPost {
    public $test;
    public function __construct() {
        $Db = new PDO('mysql:host=localhost;dbname=comment', 'root', '');
        $this->test = "yahoo";
    }

    public function Comment($Post, $Time, $Title) {

        $Post = strip_tags($_POST['Post']);
        $Time = time();
        $Title = strip_tags($_POST['Title']);

            $Messages = array('success' => 'Your comment has been added.', 'error' => 'There was a problem adding your comment.');

                if(isset($Post, $Title)) {
                    return $Messages['success'];
                } else {
                    return $Messages['error'];
                }

    }
}

$New = new SubmitPost;
var_dump($New);
于 2013-09-27T04:48:57.690 回答