php-Wood has biggerfly 1, using anonymous functions to make the form data processing become cool

Source: Internet
Author: User
about wood with BiggerflyThis is the first PHP series of wood BiggerflyIt's just a translation. forced to fly ~ up, because it's PHP, so here's the Hump namingJust like the meaning of the name, this series teaches you how to use PHP to fly of course, there are some that I used to think that forcing lattice to fly, but the result of the eggThe wood-wood brain hole has always been ... So, in the end I will do some evaluation of these code, do not want to see wood crazy, can jump other series, portals at the end of the first phase of the subject

Demand

Receive form data, format them, and then insert the database

Simulation

Suppose that the data submitted by the form is

$postData = [  [' name ' = ' name1 ', ' createtime ' = ' 2000-01-01 ', ' email ' = ' email1@example.com '],  [' Nam E ' = ' name2 ', ' createtime ' = ' 2000-02-01 ', ' email ' = ' email2@example.com '],  [' name ' = ' = ' name3 ', ' creat ETime ' + ' 2000-03-01 ', ' email ' = ' email3@example.com '],  [' name ' = ' name4 ', ' createtime ' + ' 2000-04-0 1 ', ' 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 converted to reference, reference can reduce the cost of memory Oh ~

Wood has Biggerfly

(The script is loaded ...) Wood: I do not want to write so many format method, this submission form to write a format, the next time another place also want a format, each time define a, good trouble ah, there is no. If there is a validate method for validation, or another ABC method ... Oh, my God, give me a break. Biggerfly: Would you like to try the anonymous function? Wood Wood: Huh? What is an anonymous function? Biggerfly: anonymous function portal here, walk you wood: good dizzy, you tell me directly, how to do it ... Biggerfly:

Class A {public  $data = NULL;  Public $format = NULL;  /**  * * Execute */public  function run () {    foreach ($this->data as $key = & $data) {      if (is_call Able ($this->format)) {        $method = $this->format;//must be assigned to a variable, or it will be error: The format method does not exist        $method ($data);      }    }  }  /**  * Set data array  * @param array $data  *  /Public Function SetData ($data) {    $this->data = $data ;    return $this;  }  /**  * Gets data array  * @return Array  *  /Public Function GetData () {    return $this->data;  }}

Wood: So what? How to use Biggerfly: a simulation as an example

$a = new A (), $a->setdata ($postData), $a->format = function (& $data) {  $data [' createtime '] = Strtotime ($d ata[' Createtime ');}; $a->run (); Var_dump ($a->getdata ());

run the results slightly, here will be two-dimensional array $postdata createtime date format into a timestamp biggerfly: In this way, as long as in class A to define some common methods, you can write no need to repeat the definition of functions, For example, some common rules explain

    • Is_callable Portal. If you are rigorous, you can add a Is_array judgment to make sure it is a function
    • This saves a lot of extra function definitions, and some of the commonly used methods like format and validate are embedded in Class A directly.
    • At the same time, each script that needs to format the form data may have fewer references to the class, for example, if you want to detect whether the string format is a mailbox, you might want to call a string class in each of the form processing, with Class A, you can just need a, and write all the references in a.
    • These functions that work with form data are inherently one-time (do these format methods apply to other forms of processing?) ), so the anonymous function is more consistent with its meaning.
    • Do not feel tall on the baa?

Other

    • With this kind of processing, you might be able to write code like the following, where you don't write the implementation, and you can do it easily in a 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 ()) {      Retu RN false;    } else {      return true;}}  );};
    • Anonymous functions often appear in PHP native functions, they are also called callback functions, want to learn more about anonymous functions, recommended reference manuals and related comments anonymous function "

      Transmission Door

      php-array of past period my novel spirit
  • 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.