I have the following php code:
<?php
class Session {
public function __construct() {
if (!session_id()) {
session_start();
}
if (!is_array($_SESSION['messages'])) {
$_SESSION['messages'] = [];
}
echo is_array($_SESSION['messages']);
}
public function AddMessage($msg="") {
array_push($_SESSION['messages'], $msg);
}
public function GetMessages() {
$messages = $_SESSION['messages'];
unset($_SESSION['messages']);
return $messages;
}
}
$session = new Session();
?>
And the output of $session->AddMessage('message') is:
1
Warning: array_push() expects parameter 1 to be array, null given in ...\Session.php on line 16
so is_array clearly says that $_SESSION['messages'] IS an array. What am I missing?
session_id is a function, so it should be:
if (!session_id()) { //Not just session_id
session_start();
}
If you would have turned on error reporting it might have thrown:
Notice: Use of undefined constant session_id - assumed 'session_id'
Note: OP has edited the original question