1

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.

Screenshot of the error

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>
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
Gabriel
  • 21
  • 2
  • It's a warning, not an error: https://phoenixnap.com/kb/php-error-types – bloodyKnuckles Jun 16 '22 at 09:06
  • you need to include the definition of the class before `session_start()`. Usually classes are contained in standalone php files and including that would require `include 'person.php';` ... but that depends on if you are using autoloading strategies or frameworks for your application – Diego D Jun 16 '22 at 09:14
  • @DiegoDeVita ...even though the object is in a `SESSION` variable? – bloodyKnuckles Jun 16 '22 at 09:17
  • Does this answer here help? [Storing objects in PHP session](https://stackoverflow.com/a/1442271/2743458) – bloodyKnuckles Jun 16 '22 at 09:18
  • @bloodyKnuckles I'm familiar with symfony and I'm not very much into vanilla php .. but so far that's what emerged https://stackoverflow.com/questions/20664257/the-script-tried-to-execute-a-method-or-access-a-property-of-an-incomplete-objec ... and it makes sense to have the class definition before the deserialization. But can't verify with my own eyes – Diego D Jun 16 '22 at 09:22
  • I solve it by putting my class Person to another php file and as @DiegoDeVita saids, I then put include 'person.php' on the other php files so i can store and also loop on each person. Thanks all for the feedbacks. – Gabriel Jun 16 '22 at 09:23
  • _Side note:_ Please add all error messages to the question as text, not images. People tend to search for error messages and SO (and google) can only index the error messages (and thus help others find this question) if they are added as text. – M. Eriksson Jun 16 '22 at 09:36
  • Will keep that in mind. Thanks for feedback. – Gabriel Jun 16 '22 at 11:03

0 Answers0