How to create a bug report in PHP

How to write error reporting modules in PHP? Let's say I want to write a function in PHP: 'bool isDuplicateEmail ($ email)'. In this function, I want to check if email is present in the database. He will return the "truth" if it exists. Else 'false'.

Now query execution can also fail. At this time, I want to tell the user "Internal error."

The function should not die with a typical mysql error: die (mysql_error (). My web application has two interfaces: browser and email (you can perform certain actions by sending an email). In both cases, it should report an error in good aesthetics. Should I really use exception handling for this?

Can someone point me to some good PHP project where I can learn how to create a reliable PHP web application?

+3
source share
4 answers

In my PHP projects, I tried several different clock cycles. I came up with the following solution, which seems to work well for me:

First, any major PHP application that I write has some sort of central singleton that controls data and behavior at the application level. Object "Application". I mention here because I use this object to collect the generated feedback from every other module. The rendering module may request the application object for feedback, which it considers should be displayed to the user.

, . , "AddError (, , )" "GetErrors()" "ClearErrors". "AddError" : () ( "global" ), .

, , :

, 'Object' : AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount , , HasError

// $GLOBALS['app'] = new Application();

class MyObject extends Object
{
     /**
      * @return bool Returns false if failed
      */
     public function DoThing()
     {
          $this->ClearErrors();
          if ([something succeeded])
          {
              return true;
          }
          else 
          {
              $this->AddError(ERR_OP_FAILED,"Thing could not be done");
              return false;
          }              
     }
}

$ob = new MyObject();
if ($ob->DoThing()) 
{
   echo 'Success.';
}
else 
{
   // Right now, i may not really care *why* it didn't work (the user
   // may want to know about the problem, though (see below).
   $ob->TrySomethingElse();
}

// ...LATER ON IN THE RENDERING MODULE
echo implode('<br/>',$GLOBALS['app']->GetErrorsAsStrings());

, , :

  • , , ,
  • , , , .
  • , , . "" , . .
  • ( ) , , .
+3

MVC, - / , ( /).
-, :)

+1

, Zend Framework, .

0

php. , .

, , PHP , ( ).

0

Source: https://habr.com/ru/post/1703079/


All Articles