-1

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?

Undry
  • 427
  • 2
  • 15
  • 1
    You’re not `return`ing anything, and you’re not trying to output the return value of the method, but the entire class. – deceze Apr 20 '20 at 06:36
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 22 '20 at 19:27

2 Answers2

1

1) You need to put

return $result;

at the end of your keencheck() function, so that it actually returns a value. Right now it doesn't return anything.

2) You need to echo the value returned from calling that function. Right now you're trying to echo your entire object, which is an instance of the keens class.

$interval=new keens();
$interval->topicid = 1;
echo $interval->keencheck();
ADyson
  • 57,178
  • 14
  • 51
  • 63
0

Try like this list() function : The list() function is used to assign values to a list of variables in one operation.

Note: This function only works on numerical arrays.

public function keencheck(){
    /* your code */
    return array($result, $check);
}

$interval = new keens();
$interval->topicid=1;
list($result, $check) = $interval->keencheck();

echo $result;
echo $check;
Bhavik Hirani
  • 1,996
  • 4
  • 28
  • 46
  • It usually helps to explain what you have done as there may be various changes in a block of code to fix the issue. – Nigel Ren Apr 20 '20 at 06:51
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 22 '20 at 19:27