0

我是 PHP 开发新手,但我有 Java 开发经验。在java中,我们可以将classA的对象传递给classB。然后在classB中我们可以访问classA的方法。我在 PHP 中寻找类似的东西。

在我的测试项目中,我有一个 Vote 类。

    <?php

class Vote {    
    private $DeviceID;
    private $FullName;
    private $Rate;
    private $Comment;
    private $PublishTime;

    public function __construct($deviceId, $fName, $rate, $comment) {
        $this->DeviceID = $deviceId;
        $this->FullName = $fName;
        $this->Rate = $rate;
        $this->Comment = $comment;

        if(function_exists('date_default_timezone_set'))
            date_default_timezone_set('Asia/Tehran');
        $date = new DateTime();
        $this->PublishTime = $date->getTimestamp();
    }

    public function getDeviceID() {
        return $this->DeviceID;
    }

    public function getName() {
        return $this->FullName;
    }

    public function getRate() {
        return $this->Rate;
    }

    public function getComment() {
        return $this->Comment;
    }

    public function getPublishTime() {
        return $this->PublishTime;
    }

    public function setDeviceID($deviceId) {
        $this->DeviceID = $deviceId;
    }

    public function setFullName($name) {
        $this->FullName = $name;
    }

    public function setRate($rate) {
        $this->Rate = $rate;
    }

    public function setComment($comment) {
        $this->Comment = $comment;
    }

    public function setPublishTime($publishTime) {
        $this->PublishTime = $publishTime;
    }

    public function toString() {
        echo '<p><b>Device ID:</b>'.$this->getDeviceID().'<br />';
        echo '<b>User Name:</b>'.$this->getName().'<br />';
        echo '<b>Rate:</b>'.$this->getRate().'<br />';
        echo '<b>Comment:</b>'.$this->getComment().'<br />';
        echo '<b>Time:</b>'.$this->getPublishTime().'<br /></p>';
    }
}
?>

我有另一个用于读取和写入数据库的课程。

<?php
    require_once 'class.vote.php';

class DBHandler {
    private $DB_HOST = "localhost";
    private $DB_USER = "root";
    private $DB_PASS = "";
    private $DB_NAME = "test";
    private $mysqli;

    public function __construct() {
        // CONNECT TO THE DATABASE
        $this->mysqli = new mysqli($this->DB_HOST, $this->DB_USER, $this->DB_PASS, $this->DB_NAME);
        if (mysqli_connect_errno()) {
            throw new Exception("Unable to connect to the database. Error number: " . $this->mysqli->connect_errno);
    }
    }

    public function __destruct() {
        //We are done with the database. Close the connection handle.
        $this->mysqli->close();

        echo 'DBHandler closed';
    }

    public function writeToDB(Vote $vote) {
        $query = "INSERT INTO vote (DeviceID, FullName, Rate, Comment, PublishTime)
            VALUES ('$vote->getDeviceID()', '$vote->getName()', '$vote->getRate()',
                '$vote->getComment()', '$vote->getPublishTime()')";
        $result = $this->mysqli->query($query);
        echo '$result';
        /* free result set */
        $result->free();
    }
}
?>

我的问题是writeToDB(Vote $vote)功能。当我运行项目时,将显示以下错误。任何建议将不胜感激。谢谢

Notice: Undefined property: Vote::$getDeviceID in F:\Software\NetBeans\xampp\htdocs\PHPAndroidAPI\class.dbhandler.php on line 32

Notice: Undefined property: Vote::$getName in F:\Software\NetBeans\xampp\htdocs\PHPAndroidAPI\class.dbhandler.php on line 32

Notice: Undefined property: Vote::$getRate in F:\Software\NetBeans\xampp\htdocs\PHPAndroidAPI\class.dbhandler.php on line 32

Notice: Undefined property: Vote::$getComment in F:\Software\NetBeans\xampp\htdocs\PHPAndroidAPI\class.dbhandler.php on line 33

Notice: Undefined property: Vote::$getPublishTime in F:\Software\NetBeans\xampp\htdocs\PHPAndroidAPI\class.dbhandler.php on line 33

=============

更新

我正在使用以下代码来测试我的代码:

<?php
    require_once 'class.vote.php';
    require_once 'class.dbhandler.php';

    $deviceId = $_GET["p1"];
    $fName = $_GET["p2"];
    $rate = $_GET["p3"];
    $comment = $_GET["p4"];

    try {
        // Create Vote object based on parameters
        $objVote = new Vote($deviceId, $fName, $rate, $comment);
//        $objVote->toString();

        $objDBHandler = new DBHandler();
        $objDBHandler->writeToDB($objVote);

    } catch (Exception $e) {
        die("There was a problem: " . $e->getMessage());
    }

?>
4

2 回答 2

2

据我所知,函数不能在“双引号”内工作,只能在变量内工作。因此,PHP 正在寻找一个$vote->getDeviceID变量而不是一个$vote->getDeviceID()函数。您应该将 SQL 更改为,... VALUES ('".$vote->getDeviceID()."', ...以便函数在引号之外。

此外,当数据来自用户输入时,您应该特别关注SQL 注入。

于 2013-01-27T18:38:06.230 回答
2

您可以使用如下 {} 语法

$query = "INSERT INTO vote (DeviceID, FullName, Rate, Comment, PublishTime)
        VALUES ('{$vote->getDeviceID()}', '{$vote->getName()}', '{$vote->getRate()}',
            '{$vote->getComment()}', '{$vote->getPublishTime()}')";
于 2013-01-27T18:45:21.520 回答