phpjsonstdclass

Notice: Undefined property: stdClass:: with null json value


my test.json files :

{
  "data1": "test1",
  "data2": "test2",
  "data3": "test3",
  "data4": "test4",
  "data5": "test5",
  "data6": {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key4": "value4",
    "key5": "value5",
    "key6": "value6",
    "key7": "value7"
  },
  "data7": "test7"
}

my index.php :

<?php

/* DEBUG FUNCTION */
ini_set("display_errors", "1");
error_reporting(E_ALL);

require 'test_controller.php';
$controller = new Controller();
$controller->dispatch();

my test_controller.php :

<?php
require 'test_view.php';
require 'test_model.php';
class Controller
{
protected $view;
protected $model;
    public function __construct()
    {
        $this->view = new TestView();
        $this->model = new TestModel();
    }
    public function dispatch()
    {
        $json = $this->model->getAPI();
        $this->view->displayHome($json);
    }
}

my test_model.php :

<?php

class TestModel 
{
    public function __construct()
    {
    }
public function getAPI()
{
    $data = file_get_contents('test.json');
    $json = json_decode($data);
    return $json;
}
}

my test_view.php :

<?php
class TestView
{
    public function __construct()
    {           
    }    
    public function parseJson($datas){

        $value = '&#8709;';
        $data = (isset($datas)) ? $datas : $value ;
        return $data;    
    }    
    public function displayHome($datas) {    
        $page = $this->parseJson($datas->data1);
        $page .= '<br>';
        $page .= $this->parseJson($datas->nodata);
        $page .= '<br>';
        $page .= $this->parseJson($datas->data6->key1);
        $page .= '<br>';
        $page .= $this->parseJson($datas->data6->nokey);
        $page .= '<br>';
        echo $page;
    }
}

The result is :

Notice: Undefined property: stdClass::$nodata in /home/meteobel/networkbell.com/mbell/test/test_view.php on line 21

Notice: Undefined property: stdClass::$nokey in /home/meteobel/networkbell.com/mbell/test/test_view.php on line 25

test1 ∅ value1 ∅

So my question is why I have this error result: "Notice: Undefined property: stdClass::"

Of course I simplified my problem as much as possible in this example (keeping the MVC architecture), but in my initial script I have many null values in my json for which I still want to display something (here ∅ = '&#8709;') without generating an error. I think it also slows down my page processing as if PHP was thinking in a vacuum

Thanks


EDIT :

I resolve the problem with :

$page .= isset($datas->nodata) ? $datas->nodata : '&#8709;';

For a generic function and no declare all $datas in isset, we can use eventually

$page .= $this->parseJson('$datas->nodata');

with eval() function

https://www.php.net/manual/en/function.eval.php


Solution

  • The problem is that the expression you pass to your function is evaluated before the function is called, and the function just sees the value, so by the time you check with isset() the warning was already been raised.

    The simplest solution is to not use a function at all, and use the "null coalescing operator" to keep the code short:

    $page .= $datas->nodata ?? '&#8709;'
    

    You could also put the substitution in a constant or function call to avoid pasting it each time:

    const NA='&#8709;';
    $page .= $datas->nodata ?? NA;