PHP-wood biggerFly1. use anonymous functions to make form data processing cool

Source: Internet
Author: User
PHP-wood biggerFly1. use anonymous functions to make form data processing cool About Wood having biggerFlyThis is Mu's first PHP series. BiggerFlyThe translation is Force GFEI ~ StartBecause it is PHP, so here is Camper nameJust like the name expression, this series teaches you how to use PHP Of course, there are some of the results that I once thought were amazing.Mu's brain holes have always been ...... So, in the end, I will make some comments on these codes. if I don't want to look at Mu crazy, I can jump to another series. at the end of the portal, I will enter the topic of the first phase.

Requirement

Receive form data, format and process them, and insert them to the database.

Simulation

Assume that the data submitted in the form is

$postData = [  ['name' => 'name1', 'createtime' => '2000-01-01', 'email' => 'email1@example.com' ],  ['name' => 'name2', 'createtime' => '2000-02-01', 'email' => 'email2@example.com' ],  ['name' => 'name3', 'createtime' => '2000-03-01', 'email' => 'email3@example.com' ],  ['name' => 'name4', 'createtime' => '2000-04-01', 'email' => 'email4@example.com' ],];
Normal practice
function format( $data ) {  $format = $data;  $format['createtime'] = strtotime( $data['createtime'] );  return $format;}

Of course, the above can also be replaced with references,References can reduce memory overhead ~

Wood has biggerFly

(Loading the script ......) Mu: I don't want to write so many format methods. I need to write a format for the form submitted this time. I need to define a format for the next time and define a format for each form. this is very troublesome. Is there any format. If there is another validate method for verification, or other abc methods ...... Oh, my God. excuse me! biggerFly: Do you want to try the anonymous function? Mu: ha? What is an anonymous function? BiggerFly:Anonymous functionsThe portal is here, and you will be taken away from Mu: It's so dizzy. you can tell me how to do it directly ...... BiggerFly:

Class A {public $ data = NULL; public $ format = NULL;/*** run */public function run () {foreach ($ this-> data as $ key => & $ data) {if (is_callable ($ this-> format) {$ method = $ this-> format; // you must assign a value to a variable. Otherwise, an error is returned: the format method does not exist $ method ($ data );}}} /*** set the data array * @ param array $ data */public function setData ($ data) {$ this-> data = $ data; return $ this ;} /*** get the data array * @ return array */public function getData () {return $ this-> data ;}}

Mu: So? How to use biggerFly:SimulationFor example

$a = new A();$a->setData( $postData );$a->format = function( &$data ) {  $data['createtime'] = strtotime( $data['createtime'] );};$a->run();var_dump( $a->getData() );

Running resultHere, we will convert the date format of the createtime of the two-dimensional array $ postData to the timestamp biggerFly: In this way, as long as some common methods are defined in Class, you can write functions that do not need to be repeatedly defined, such as some common rules.Explanation

  • Is_callable Portal. If it is more rigorous, you can add an is_array judgment to ensure that it is a function.
  • This saves A lot of additional function definitions. some common methods such as format and validate can be directly embedded in Class.
  • At the same time, many classes may be referenced in every script that needs to format form data. for example, if you want to check whether the string format is email, you may need to call A String class in each form processing place. with Class A, you can just need A class and write all the references in Class.
  • These functions that process form data are originally one-time (are other form data processing methods applicable to these format methods ?), Therefore, the use of anonymous functions is more consistent with its meaning.
  • Don't you think it's too big?

Others

  • Through such processing, you may also write the following similar code. here, the implementation is not written, and it is easy to implement it in the format.
    $a->validate = function( &$data ) {$data['email'] = array( 'email', 'unique' );$data['name'] = array( 'required', 'unique' );$data['createtime'] = array( 'datetime', function($time) {    if ( strtotime( $time ) >= time() ) {      return false;    } else {      return true;    }  } );};
  • Anonymous functions are frequently used in php native functions. They are also called callback functions. to learn more about anonymous functions, I suggest you refer to the manual and related comments to the anonymous function "portal to PHP-array my novels

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.