I made a object "Person" and I'm storing new object of "Person" in a session variable. But whenever I loop each person from the session variable and use var_dump() to view/show only the name of each person. I get this error. How to solve this error? Thanks.
Here is where I store new object :
<?php
session_start();
class Person
{
public $name;
public $age;
public function __construct()
{
}
}
if (!isset($_SESSION['persons'])) {
$_SESSION['persons'] = array();
}
if (isset($_POST['submit'])) {
$name = isset($_POST['name']) ? $_POST['name'] : "onbekend";
$age = isset($_POST['age']) ? $_POST['age'] : "onbekend";
$new_person = new Person();
$new_person->name = $name;
$new_person->age = $age;
$_SESSION['persons'][] = $new_person;
return header('Location: list.php');
}
Here is where I loop each person and get person's name:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
session_start();
$data = isset($_SESSION['persons']) ? $_SESSION['persons'] : "No persons.";
if (isset($data)) {
foreach ($data as $person) {
var_dump($person->name);
echo "<br>";
}
}
?>
<a href="index.php">return</a>
</body>
</html>