Build your own PHP Framework (3) and build a php framework _ PHP Tutorial

Source: Internet
Author: User
Tags php exception handling
Build your own PHP Framework (3) and build a php framework. Build your own PHP Framework (III), build a php framework, and then improve your PHP Framework. The main content of this update is: introduced the exception handling mechanism, improved exception and error handling, set up your own PHP Framework (3), and set up the php Framework
Statement

Then I improved my PHP Framework. The main content of this update is:

  • Introduced the exception handling mechanism
  • Improved exception and error handling
  • Ing between data tables and Model classes
Exception handling

Exception handling: exception handling is a mechanism in programming languages or computer hardware used to handle exceptions in software or information systems (that is, exceptions that exceed certain special conditions for normal program execution processes)

Exception handling is used to handle exceptions in a program. although it is an "exception status", it is still expected by the programmers, in fact, the program exception handling can be completely replaced by the 'If else' statement, but the exception handling naturally has its advantages.

My personal summary has the following advantages:

  • The process can be quickly terminated, the system status can be reset, and the variables and memory usage can be cleared. in a common WEB application, after a request is completed, fast cgi automatically clears the variables and context, however, if you execute the daemon script in PHP command line mode, the effect will be very convenient.
  • A large number of if else statements make the code complex and difficult to understand. using exception handling can make the program logic clearer and easier to understand. after all, the exception handling entry is only one catch statement.
  • If a function in a program has an exception result or condition, if the return method of the function is used to return the exception information, the return judgment should be made every time. With exception handling, we can assume that all returned information is normal, avoiding a lot of code duplication.

Although it may be slightly less efficient to put the code in a try catch block, compared with these advantages, this consumption is nothing. So how to use PHP exception handling?

PHP has an internal Exception class so that we can throw an Exception by instantiating the Exception class. We put the code in the try statement and then use catch to catch the Exception thrown in the try code block and handle the exception. We can also use the finally statement block after the catch code segment. the code of the finally code block will be executed no matter whether an exception exists. try catch statements are like the following code:

try{    throw new Exeption('msg'[,'code',$previous_exeception]);}catch(Exeption $var) {    process($var);}catch(MyException $e){    process($e)}finally{    dosomething();}

Note the following when using try catch statements:

  • When an exception is thrown, an exception class is instantiated. this exception class can be defined by yourself. However, in a catch statement, we need to specify the class name of the exception object to be captured, and only exception objects of specific classes can be captured. of course, we can catch an exception base class (PHP built-in exception class) at the end to ensure that exceptions can be captured.
  • When an exception is thrown, the program is terminated, and the code is traced back to find the first catch statement that can capture it. the try catch statement can be nested, as shown in the code above, the cacth statement can be defined multiple times.
  • The finally block is executed after the try catch block ends. even if return is used in the try catch block, the program is not executed until the end.
Exception handling in the framework

After talking about so many exceptions (of course, these are explained to understand and use the framework), how can we implement the framework?

Override exception class

We can rewrite the exception class and improve its internal method:


  

As mentioned above, the final method cannot be rewritten. In addition, we can define our own methods, such as logging exception logs, like my custom log method, in the catch code block, you can directly use$e->logTo record an exception log.

How to register a global exception

We can use set_exception_handler ('exceptionhandler') to globally capture exceptions that are not caught by catch blocks. this exception handling function requires an exception handling object to be passed in, so as to analyze the exception handling information, this prevents the system from being humanized and enhances the robustness of the framework.

Function exceptionHandler ($ e) {echo 'has an uncaptured exception, in '. $ e-> getFile (). ". $ e-> getLine (). "line! ";}
Other global functions

By the way, other global processing functions:

  • Set_shutdown_function ('shutdownhandler') is used to execute the function at the end of the script. This function is automatically called even after the ERROR is completed.
  • Set_error_handler ('errorhandler') is automatically called when an error occurs in PHP. Note that the error is called only after an error function is registered. The function parameter format should be ($ errno, $ errstr, $ errfile, $ errline );

However, note that these global functions must be defined before the code segment and then registered.

ActiveRecord cord ing between data tables and Model classes

The first time I used the ActivceRecord class of yii2, I thought it was very convenient. I just needed to define the attribute with the same name of the field and call the save method. (amazing) how did I implement it? I read the source code, understand its general implementation process (base class ).

Conclusion

I feel like I haven't written a blog for a long time. 'Graduation 'is a little complicated for a person similar to a specialist study method. I keep my nostalgia for the school and start on.

The more I learn, the more I feel that I do not know enough. When I look at some PHP Framework source code, I sometimes feel that I am still far away, the overall sense and layout, it may take time and experience to accumulate.

Because the application of the framework is not very closely related to your current work, and you have recently worked hard to learn something about the underlying programming class, the framework series may be somewhat 'put ', write something else... I have been planning to change my location for the past two days. I ran to see the house. forgive me for being a little short ..

Haha, welcome to continue to follow my blog. well, I have been paying attention to it all the time.

Listen (3), build a php framework, and then improve your PHP Framework. The main content of this update is: introduced the exception handling mechanism, improved exception and error handling...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.