Handle messages PHP native errors / alerts

3

I have a mini framework to generate my elements, make js / ajax requests interactions and interact with BD MySQL based on some patterns.

I've been using it for quite some time now, but I'm looking forward to a little increase and to insert a kind of debugger into the framework.

A very simple example:

  public function mask( string $mask =  'undefined')
    {
        $this->mask = ' data-input-mask="' . $mask . '" ';
        return $this;
    }

This method is within the class responsible for the assembly of the element 'input' , and grouping with the other methods the call is made as follows:

$input = new \vidbModel\inputElement();

$field = $input
        ->type('text')
        ->field('cpf')
        ->mask('cpf')
        ->name('cpf')
        ->validator('cpf')
        ->value(null)
        ->input(0);

In this example, if I call the method and insert an array in the ->mask initiator, I get the following error message:

  

Fatal error: Uncaught TypeError: Argument 1 passed to vidbModel \ inputElement :: mask () must be of the type string, given in /opt/lampp/htdocs/VIDB/docs/inputs.php on line 25 and defined in /opt/lampp/htdocs/VIDB/model/inputElement.php:78 Stack trace: # 0 /opt/lampp/htdocs/VIDB/docs/inputs.php(25): vidbModel \ inputElement-> mask Array) # 1 {main} thrown in /opt/lampp/htdocs/VIDB/model/inputElement.php on line 78

What I want to do is this:

Receiving this error so that I can treat and bring a friendlier message about the error, convert that native message written above into something like:

  

Error encountered [0001], an array was passed as a parameter to the mask () method, the input must be done with a string only, to check an example: link

I checked one but found nothing about how to do this.

Is it possible to handle native PHP errors?

Is there a translation for PHP errors? (Although I would not be interested in this, just out of curiosity)

    
asked by anonymous 10.08.2017 / 16:22

1 answer

4

In version 7 of PHP and later was implemented the handling of PHP errors in Try, recovering the error with Catch

In this way:

try
{
   // seu código
}
catch (Error $e)
{
    echo "Ocorreu um erro: " . $e->getMessage();
}

If your code within Try shows error above, PHP will not stop executing other code blocks, so its fatal error becomes an exception.

More news from PHP7 go to Here

Regarding the translation, I believe that it is not possible natively, only with plugins or nail itself. Since the error messages are created by the team that develops PHP and of course that English is the most comprehensive language in the world. I do not see any advantages of translating the errors because they are few used after the block has been programmed.

I hope I have helped.

    
10.08.2017 / 16:36