Edit report at https://bugs.php.net/bug.php?id=78683&edit=1
ID: 78683
Comment by: 6562680 at gmail dot com
Reported by: 6562680 at gmail dot com
Summary: [ Feature ] Add function to safely print nested
objects / arrays in Exceptions
Status: Not a bug
Type: Feature/Change Request
Package: *General Issues
Operating System: Win10
PHP Version: 7.3.10
Block user comment: N
Private report: N
New Comment:
(Any type) -> short one-line (string)
Previous Comments:
------------------------------------------------------------------------
[2019-10-18 17:38:12] requinix@php.net
I'll try this another way:
What is an example of the input you will give this function, and what output do you want to get?
------------------------------------------------------------------------
[2019-10-18 17:33:42] 6562680 at gmail dot com
print_r - has linebreaks that will be replaced to '\n' string representation, has
recursions and 2000 symbols of possible length
var_dump - same
var_export - same, use it for sync both file and config
gettype - you see 'object'. nice one. all becomes explained when you see
'object'
get_class - yes, write that damn 7-lines if/else in each exception
json_encode - could be used, because does not contain line breaks, but works as complete example
creating expectation that value could be copied. No, exception should give you short info instead of
copied object
serialize - unreadable useless thing, could be improved in caches, but not for output.
just stop deny all request you see.
------------------------------------------------------------------------
[2019-10-18 17:28:50] 6562680 at gmail dot com
I mark this "bug" as feature/change request, are you saw it?
Have you tried to print exception with var_export? Did you see these "\n" in every debug
panel that try to replace line breaks? Why did you ask me to do that? Please, review that comment if
you regard community experience
------------------------------------------------------------------------
[2019-10-18 11:38:24] requinix@php.net
print_r
var_dump
var_export
gettype
get_class
json_encode
serialize
This is a bug tracker. It is not suitable for Twitter rants.
------------------------------------------------------------------------
[2019-10-18 08:08:03] 6562680 at gmail dot com
Description:
------------
See the example
Usually it looks like exception with error message like "Something wents wrong", instead
of "your data is incorrect: {data}"
For example - in JS console you can print "anything". Every object has some string
representation. Its not realization, its only debug/output stuff.
Every person who i had asked answers like "you can extend exception to solve" - my answer
is "i can solve the problem, i want FAST AND COMFORTABLE solve the issue". I can solve it
adding argument with extending, adding Exceptions folder, there will be at least 20 lines of code,
then everywhere i should "use" that damn exception, remembmer about this dependency, and
remember that i should be inside the module. Of course i can create own function in own library or
file, so i need write composer json, and everybody should remember to composer install, know how
dependencies works and in fully example - know dependency injection at least.
Am using something like that, there is buggy, dev realization of course
```
// printval
public function printval($data) // : mixed
{
if (is_null($data)) return 'null';
if (false === $data) return 'false';
if (true === $data) return 'true';
if (is_float($var) && is_nan($var)) return 'NaN';
if (is_scalar($var) && (! (is_bool($var) || is_nan($var) || ('' === $var))))
return $data;
if (is_array($data)) {
array_walk($data, function (&$item) {
$item = $this->printval($item); // ! recursion
});
// some serialization or cutted one
return $cut_serialized;
}
if (is_resource($h) || 'resource (closed)' === gettype($h)) {
return '{ Resource #' . intval($data) . ' }';
}
if (is_string($var) || is_numeric($var) || (is_object($var) && method_exists($var,
'__toString'))) {
return strval($data);
}
if (is_object($data)) {
// reduce class length building it like scissors regarding @maxlen attribute or const
// Hello/World/Test/Foo/Bar -> Hello/.../Bar -> Hello/World/.../Bar ->
Hello/World/.../Foo/Bar
return $cut;
}
return strval($data);
}
```
Test script:
---------------
class Someclass
{
protected $data;
public function hasSomething($by) : bool
{
if (is_string($by)) return isset($this->data[ $by ]);
elseif (is_object($by)) return $this->SOME_CHECK_METHOD_RETURNS_BOOL($by);
}
public function hasSomethingOrFail($by)
{
if (! $this->hasSomething($by)) {
throw new \RuntimeException('Bad: ' . EXPECTED_FUNC($by));
}
}
}
Expected result:
----------------
function printval() {} // logval(), lognow() or something
function hash() // creates hash for variable depending on its type automatically, allows me to build
array with hash() keys to unique or merge records without tonns of foreachs.
Actual result:
--------------
There's no function like that.
You write it as you want, you extending exceptions, you should carry damn dependencies even if your
project should contain 1 file with simple logic.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78683&edit=1