0

私はPHP開発の初心者ですが、Java開発の経験があります。Javaでは、classAのオブジェクトをclassBに渡すことができます。次に、classBでclassAのメソッドにアクセスできます。PHPでこのようなものを探しています。

私のテストプロジェクトでは、投票クラスがあります。

    <?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

次のような{}構文を使用できます

$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 に答える