I’m new in PHP development but I have experience in Java development. In java we can pass an object of a classA to classB. Then in classB we have access to methods of classA. I’m looking for something like this in PHP.
In my test project, I have a Vote class.
<?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>';
}
}
?>
I have another class for reading from and writing to database.
<?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();
}
}
?>
My problem is writeToDB(Vote $vote) function. When I run the project following errors will be displayed. Any suggestion would be appreciated. Thanks
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
============
Update
I’m using following code in order to test my code:
<?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());
}
?>
As far as I can tell, functions do not work inside “double quotes”, only variables. So, PHP are looking for a
$vote->getDeviceIDvariable instead of a$vote->getDeviceID()function. You should change your SQL to... VALUES ('".$vote->getDeviceID()."', ...so the functions are outside the quotes.Also, you should care about SQL injection specially when data comes from user input.