|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-10-18 08:08 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 22:00:01 2025 UTC |
Its not a serialization. To serialize we still should pass additional argument. Its not for logging where you should get full explained output. The function is used only at info level, between the developers, in maybe notifications / exception. I dont cry that is REQUIRED AS AIR. Will be great to have this one to print "variable info" in short as we can, to help developer to explain while debugging without analyze logs. No more! It just like "console.log()" keeping in the mind that we havent interactive dropdown lists, and have only one string How would it produce a "short one-line string" for something like $_SERVER? { Array(27) } What does it produce for classes without a __toString? { Object #27 My\Awesome\Namespace } What about binary strings? Not sure: represent as is, but maybe "\0PropertyName\0 representation Control characters like newlines and tabs? "Still '\t' and '\n'. If it is a string with these chars - ok, you pass it you get it. Var_dump() creates it to format output. And how *is* recursion supposed to be handled? In this function - noway and not requested. You just display object id and classname, it will be used like this: throw new \UnexpectedValueException('Incorrect object passed: FUNC($var) => FUNC(...$allowed_list) (split with space if unpack) Could it be that maybe your logging method of sticking something into the exception message isn't good enough? And that maybe you need a more complex solution, like PSR-3? Again i know about logging. know about extending exceptions and can send my exceptions to bugtracker. I just want to skip requirement to deep debugging and log analyzing on easy validation stuff like "value not in list". When i debugging - i still remember what values i expect, i just need to short hint what value was passed (and resulted with exception). I dont need exactly deep representation and ability to copy and convert back. Just view short hint about like if you use Symfony\VarDumper but without dropdowns - only type/id/lengthString could be { String(27) "My\0BadString\27With\t\nTabs...." } // split vars with 1 space logval(...$arguments) // cut every output to maxlen then wrap with '{ <Type> [#<id>] <Cutstr> } printval(int $maxlen, ...$arguments);