new stdClass continues showing Warning "Creating default Object from Empty value"

Asked

Viewed 868 times

0

I have the following php code

    $obj = new \stdClass;

    foreach($_GET as $key => $value) {
        $obj->get->$key = $value;
    }

    foreach($_POST as $key => $value) {
        $obj->post->$key = $value;
    }

    return $obj;

The code works normally but php Warning

Warning: Creating default Object from Empty value in C: Users User Desktop Loren core Route.php on line 31

continues to be shown, even though I instantiated the stdClass

What must be done?

  • What version of php do you have running ? The obj has some use in the code above that shown ?

  • It’s not easier to do: $obj = (object) $_POST;?

1 answer

2

The code you made also lacked say that members (get and post) is also the type \stdClass?

<?php

    $obj = new \stdClass;
    $obj->get = new \stdClass; // faltou isso
    $obj->post = new \stdClass; // faltou isso

    foreach($_GET as $key => $value) {
        $obj->get->$key = $value;
    }

    foreach($_POST as $key => $value) {
        $obj->post->$key = $value;
    }

    print('<pre>');
    print_r($obj);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.