krumo icon indicating copy to clipboard operation
krumo copied to clipboard

Include handling of uninitialised objects in class

Open ggieling opened this issue 2 years ago • 3 comments
trafficstars

I have a class that with some properties that are objects themselves (e.g. datetime objects, or mysqli objects). These objects are nor immediately initialised, they will only be initialised when needed. When trying to use krumo on a object defined by a class that contains uninitialised objects as properties, an error message is generated and the script ends.

the error message is:

: Uncaught Error: Typed property PatentCollection::$dbReader must not be accessed before initialization in ..... Stack trace: #0 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(1083): ReflectionProperty->getValue() #1 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(1285): Krumo::_vars() #2 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(888): Krumo::_object() #3 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(530): Krumo::_dump() #4 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(1640): Krumo::dump()

I guess that this may be solved by testing with https://www.php.net/manual/en/reflectionproperty.isinitialized.php If a property appears to be not initialised, skip processing it.

ggieling avatar Nov 09 '23 18:11 ggieling

Interesting. I'm not familiar with non-initialized objects. I will gladly work on some code to address this if you create a small testcase that recreates this issue.

scottchiefbaker avatar Nov 09 '23 18:11 scottchiefbaker

I'm probably due to get a new version of Krumo out to address some PHP 8.2 stuff anyway, so now is a decent time to look at it.

scottchiefbaker avatar Nov 09 '23 18:11 scottchiefbaker

The issue is actually with any non-initialised property. In the below example:

<?php

include_once ($_SERVER['DOCUMENT_ROOT'].'/includes_2018/krumo-master/class.krumo.php');

class dummy {

public string $teststring = 'test';
public array $fib = [1,2,3,5,8,13];
public ?string $emptystring;

}

$test = new dummy();
// $test->emptystring = null;
krumo($test);

echo 'krumo display succesfully completed';

?>

The above code generates an error message with the following content:

:  Uncaught Error: Typed property dummy::$emptystring must not be accessed before initialization in /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php:1083
Stack trace:
#0 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(1083): ReflectionProperty->getValue()
#1 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(1285): Krumo::_vars()
#2 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(888): Krumo::_object()
#3 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(530): Krumo::_dump()
#4 /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php(1640): Krumo::dump()
#5 /srv/http/ipdev/test/krumotest.php(15): krumo()
#6 {main}
  thrown in /srv/http/ipdev/includes_2018/krumo-master/class.krumo.php on line 1083

The error message is not visible in the browser, you have to go into the source code for the page to find it in the krumo structure.

After uncommenting the line:

// $test->emptystring = null;

The page succesfully displays the object structure and displays the success message below it.

Request in increasing level of desirability: option 1) make krumo skip uninitialised properties to prevent the situation that the krumo result is not visible. Krumo is mostly used in debugging, so it is desirable that it always produces a legible result. option 2) make krumo not simply skip the uninitialised property, but include it with the remark that it is uninitialised. option 3) make krumo include the uninitialised property with a warning below the krumo result that this property is uninitialised

ggieling avatar Nov 10 '23 07:11 ggieling