Question about PHP code specifications

Source: Internet
Author: User
First, there is a function written in this way. {Code...} then, this function is called in this way. {Code...} Let's take a look at the experienced phper, which looks awkward in a few places: in the function exam (), is the return position appropriate. If the return type in the function is incorrect... first, a function is written in this way.

Function exam () {// obtain data from the database. $ Records = $ aModel-> where (['type' => 1])-> get (); if (empty ($ records) return false; $ newRecord = $ bModel-> map (); foreach ($ records as $ item) {$ newRecord-> name = $ item-> name; $ newRecord-> age = $ item-> age; try {$ bModel-> insert ($ newRecord);} catch (\ Exception $ e) {\ Log :: catch ($ e) ;}} return true ;}

Then, this function is called in this way.

If (true! = Exam () {echo "table update failed"; return false ;}

Let's take a look at the experienced phper, which looks awkward in a few of them:

  1. In the function exam (), is the return position suitable.

  2. If the return type of the function is uncertain, the result is returned after the execution is complete, and the result is returned if the execution fails and false is returned, is it suitable for similar results during the call?if(false === exam()).

  3. I feel that executing a function in the if judgment will reduce the readability of the code and wonder if such a problem exists.

Reply content:

First, there is a function written in this way.

Function exam () {// obtain data from the database. $ Records = $ aModel-> where (['type' => 1])-> get (); if (empty ($ records) return false; $ newRecord = $ bModel-> map (); foreach ($ records as $ item) {$ newRecord-> name = $ item-> name; $ newRecord-> age = $ item-> age; try {$ bModel-> insert ($ newRecord);} catch (\ Exception $ e) {\ Log :: catch ($ e) ;}} return true ;}

Then, this function is called in this way.

If (true! = Exam () {echo "table update failed"; return false ;}

Let's take a look at the experienced phper, which looks awkward in a few of them:

  1. In the function exam (), is the return position suitable.

  2. If the return type of the function is uncertain, the result is returned after the execution is complete, and the result is returned if the execution fails and false is returned, is it suitable for similar results during the call?if(false === exam()).

  3. I feel that executing a function in the if judgment will reduce the readability of the code and wonder if such a problem exists.

  1. In exam (), why does not return false directly when an exception is thrown?

  2. Generally, if the return type is bool, it is not recommended to add the third type. the method you mentioned can be judged, but it is not recommended.

  3. The readability problem is not because it is in if, but because your function name is faulty. if you want to return a result of bool type, we suggest adding an is parameter, for example:

If (false = isExam () {echo "table update failed"; return false ;}

To recommend a book, you can see:
Https://book.douban.com/subje...

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.