I'm just studying PHP and have a question.
Here is a class that I have created
class keens{
public $userid, $codeq, $codetest, $qposition, $ans, $testid, $last, $keencode, $topicid, $datekeencomplete, $datenow;
function __construct() {
$this->userid = $_SESSION['id'];
}
public function keencheck(){
$sql="SELECT MAX(date) FROM `keencomplete` WHERE `topic_id`=$this->topicid";
$run=mysqli_query(DBConnect::$conn, $sql) or die(mysqli_error());
$row=mysqli_fetch_array($run);
$this->datekeencomplete=new DateTime($row[0]);
$this->datenow=new DateTime(null, new DateTimeZone('Asia/Almaty'));
$interval=date_diff($this->datekeencomplete, $this->datenow);
$result=$interval->format('%d');
$check=is_string($result)? 'true':'false';
echo $result;
echo $check;
}
}
When I check the $result it shows that it contains "2" and it is a string "2true".
However when I try to return $result and use it in this code
$interval=new keens();
$interval->topicid=1;
$interval->keencheck();
echo $interval;
I get the following error: Recoverable fatal error: Object of class keens could not be converted to string in C:\xampp\htdocs\Ikeen\views\v_tests.php on line 16
The print_r($interval) shows following, this is not what I return, right?
keens Object ( [userid] => 48 [codeq] => [codetest] => [qposition] => [ans] => [testid] => [last] => [keencode] => [topicid] => 1 [datekeencomplete] => DateTime Object ( [date] => 2020-04-17 12:39:51.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [datenow] => DateTime Object ( [date] => 2020-04-20 12:32:50.990006 [timezone_type] => 3 [timezone] => Asia/Almaty ) )
Sorry if I do something stupid. Any ideas why I can't get the return value of a function within a class?