Another approach is to change the call. If we want to check if a constant exists, this line will be too late. $response->status(ApiError::INVALID_REQUEST);
The php interpreter also checks for the existence of a constant and will die with Fatal Error. This is not fun with try ().
Therefore, I would suggest using a string as a parameter to check for existence using certain () and constants ()
class ApiError { const INVALID_REQUEST = 200; } class Response { public function status($status) { if (!defined('ApiError::'.$status)) { return false;
Then use will look like this:
$response = new Response(); $response->status('INVALID_REQUEST');
The bad news is that there are no type hints for this solution.
source share