在PHP中,当对象传递给其他类时,如何访问该对象的函数?

在PHP中,当对象传递给其他类时,如何访问该对象的函数?,php,Php,我是PHP开发新手,但我有Java开发经验。在java中,我们可以将类a的对象传递给类B。然后在classB中,我们可以访问classA的方法。我正在PHP中寻找类似的东西 在我的测试项目中,我有一个投票班 <?php class Vote { private $DeviceID; private $FullName; private $Rate; private $Comment; private $PublishTime;

我是PHP开发新手,但我有Java开发经验。在java中,我们可以将类a的对象传递给类B。然后在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';
    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());
    }

?>

据我所知,函数在“双引号”中不起作用,只在变量中起作用。因此,PHP正在寻找一个
$vote->getDeviceID
变量,而不是
$vote->getDeviceID()
函数。您应该将SQL更改为
。。。值(““$vote->getDeviceID()”,…
,因此函数不在引号内


另外,当数据来自用户输入时,您应该特别注意。

据我所知,函数不在“双引号”中工作,只在变量中工作。因此,PHP正在寻找一个
$vote->getDeviceID
变量,而不是
$vote->getDeviceID()
函数。您应该将SQL更改为
…value('s>)“$vote->getDeviceID()。”,…
因此函数不在引号内


另外,当数据来自用户输入时,您应该特别注意。

您可以使用{}语法,如bellow

$query = "INSERT INTO vote (DeviceID, FullName, Rate, Comment, PublishTime)
        VALUES ('{$vote->getDeviceID()}', '{$vote->getName()}', '{$vote->getRate()}',
            '{$vote->getComment()}', '{$vote->getPublishTime()}')";

您可以使用{}语法,如bellow

$query = "INSERT INTO vote (DeviceID, FullName, Rate, Comment, PublishTime)
        VALUES ('{$vote->getDeviceID()}', '{$vote->getName()}', '{$vote->getRate()}',
            '{$vote->getComment()}', '{$vote->getPublishTime()}')";

您使用的“投票”无效,因为函数需要一个参数,所以请尝试在$variabel中初始化投票。这样它会传递所需的输出。如何将投票对象传递给DB类?@brenjt,请检查更新。谢谢。投票“由于函数需要一个参数,因此您的使用无法工作,因此请尝试在$variabel中初始化投票。这样它将传递所需的输出。您如何将投票对象传递给DB类?@brenjt,请检查更新。谢谢。
$query = "INSERT INTO vote (DeviceID, FullName, Rate, Comment, PublishTime)
        VALUES ('{$vote->getDeviceID()}', '{$vote->getName()}', '{$vote->getRate()}',
            '{$vote->getComment()}', '{$vote->getPublishTime()}')";