Код: Выделить всё
class ValueObject
{
public function __construct(public string $value)
{
}
public function __toString(): string
{
return $this->value . "; this I can control";
}
}
Код: Выделить всё
$object = new ValueObject('42');
print_r([
(int) $object, // should throw
(float) $object, // should throw
(bool) $object, // should throw, but only for this class instances, yet ok if not possible
(string) $object, // I can make it throwable via `::__toString()`
]);
< /code>
будет печатать: < /p>
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 42; this I can control
)
< /code>
Я вижу предупреждения для Float и int: < /p>
PHP Warning: Object of class Application\ValueObject could not be converted to int
PHP Warning: Object of class Application\ValueObject could not be converted to float
< /code>
Все же на первом взгляде код появляется так, как будто работает, но он использует бессмысленные данные. < /p>
Как превратить эти предупреждения в ошибку? /p>
либо для всех этих отливок Int и Float, но лучше: только для этих самых классов. ) Я на php 8.0.30.function exception_error_handler(int $errno, string $errstr, string $errfile = null, int $errline) {
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler(__NAMESPACE__ . "\\exception_error_handler");
< /code>
Тем не менее, я скорее имею только определенные предупреждения, которые могут быть преобразованы в ошибку, не все из -за устаревшей кодовой базы и поддержания воздействия как можно меньше. < /p>
Следовательно, хочу бросить ошибку только для определенных экземпляров.
Подробнее здесь: https://stackoverflow.com/questions/794 ... -warning-i
Мобильная версия