1

I would like to ask if this is possible:

I have a class with a private constructor which of course throws a fatal error if I try to instantiate that class from an invalid context (outside the class), can I catch this fatal error and the name of the class and then throw an exception like:

// catch the private constructor from invalid context fatal error
// and store somehow the name of the class inside $class
// and then:
throw new UninstantiableClassException("The class " . $class . " cannot be instantiated");

Is that possible, or basing on this post I have just found it's not? -> PHP : Custom error handler - handling parse & fatal errors

If it is not possible, should I make the constructor public and then throw and exception inside of it like this?:

public function __construct() {
   throw new UninstantiableClassException("The class " . get_class($this) . " cannot be instantiated");
}
Community
  • 1
  • 1
tonix
  • 6,671
  • 13
  • 75
  • 136
  • Actually I don't think this is a duplicate question, I have read @user259973 answer on that post. In his answer he says that register_shutdown_function should be used, but even calling `register_shutdown_function()` the Fatal error will still appear even on the screen before the register_shutdown_function() is called, so my question was if there was a method to make that fatal error to *not* appear on the screen when something goes wrong, but instead make a custom fatal error handler... I hope you understand it. – tonix Nov 03 '14 at 09:47

1 Answers1

2

Calling a class with a private constructor will result in a fatal error, because the constructor is always called. PHP doesn't have built-in functions to catch fatals, but you could take a look here to see how to catch fatal errors.

How do I catch a PHP Fatal Error

This example should give you a good impression of how to use register_shutdown_function

class Test
{
    private function __construct()
    {
        throw new UninstantiableClassException("The class " . get_class($this) . " cannot be instantiated");
    }
}

register_shutdown_function('shutdownFunction');

function shutDownFunction() {
    $error = error_get_last();
    if ($error['type'] == 1) {
        $error['message'] = 'Your message here';
        var_dump($error);
    }
}

$foo = new Test();

The output of var_dump($error) will be:

array (size=4)
    'type' => int 1
    'message' => string 'Your message here' (length=17)
    'file' => string '/var/www/test.php' (length=21)
    'line' => int 21
Community
  • 1
  • 1
  • Thank you for the post, so if I implement a register_shutdown_function() such the guy of the post you liked me I with error_get_last() I can catch every error? Then I should look if the error is of type 1 (E_ERROR, thus a fatal error) and search for a pattern in the ['message'] field like `~^Call\sto\sprivate*?::__construct()\sfrom invalid\scontext$~`, right? Anyway is there still a way to make the original E_ERROR fatal error not appear but instead show only a custom message (I mean without changing the conf php.ini file)? – tonix Oct 30 '14 at 10:58