嘿,我是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>
答案 0 :(得分: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"]);
答案 1 :(得分: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);