AddMessage2Log(date('d-m-Y H:i:s'), '---');
Задача отследить баг: при вводе в поле input появляется клава, и поле прячется.
<?php
class A{}
$class_name = 'A';
$code = <<<CODE
class B extends $class_name{}
CODE;
eval($code);
if(new B() instanceof A){
echo 'Ok';
}
<?php
class Response {
public function sendResponse(array $data) {
echo json_encode($data);
return true;
}
public function sendError(string $message) {
echo json_encode(['error' => $message]);
return false;
}
}
class ErrorHandler {
private $response;
public function __construct(Response $response) {
$this->response = $response;
}
public function handlerError($error_code, $message, $file = null, $line = null) {
if ($error_code) {
throw new Exception($message, $error_code);
}
return true;
}
public function handlerShutdown() {
$error = error_get_last();
if ($error) {
$this->handlerException(new Error($error['message'], $error['type']));
}
exit(0);
}
public function handlerException(Throwable $exception) {
if (ob_get_length()) {
ob_end_clean();
}
if (!headers_sent()) {
return $this->response->sendError($exception->getMessage());
}
return true;
}
}
$response = new Response();
$handler = new ErrorHandler($response);
register_shutdown_function([$handler, 'handlerShutdown']);
set_error_handler([$handler, 'handlerError']);
set_exception_handler([$handler, 'handlerException']);
#region Logic
$cnt = 0;
if ($cnt === 0) {
throw new Exception("Нулевое количество");
}
#endregion
$response->sendResponse([
'message' => "Всё окей"
]);
class A {
/**
* A constructor.
* @throws Exception
*/
public function __construct()
{
// $cnt откуда-то берется...
$cnt = 0;
if ( $cnt < 1 ) {
throw new Exception("нулевое количество");
}
}
}
class B {
private $errors = array();
public function run()
{
try {
$a = new A();
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
return array("errors"=>$this->errors);
}
}
class A {
/**
* A constructor.
*/
public function __construct()
{
}
/**
* @throws Exception
*/
public function init()
{
// $cnt откуда-то берется...
$cnt = 0;
if ( $cnt < 1 ) {
throw new Exception("нулевое количество");
}
}
}
class B {
private $errors = array();
public function run()
{
try {
$a = new A();
$a->init();
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
return array("errors"=>$this->errors);
}
}