Selective variable declaration in php

Source: Internet
Author: User
I don't know how to selectively declare variables in php. let's look at my process first.
Start:
$ Field = ['A', 'BB ', ......]; # dozens of 20 + $ data = array (); foreach ($ field as $ v) {switch ($ v) {case "aa ": $ data [$ v] = get_aa (); break ;.......}}

Later
If $ field has only one value zz
In this way, the program will compare and judge from the first case one by one, which is not good.
Then I will
$ Field = ['A', 'BB ', ......]; # $ aa = function () {return get_aa () ;};# add the operation code to the anonymous function $ data = array (); foreach ($ field as $ v) {$ data [$ v] =$ $ v ();}

# This frees you from a lot of judgment and the problem arises.
# In this case, many variables such as $ aa and $ bb need to be declared in advance, so I will wonder if $ field = ['zz '] can I choose to load only $ zz?
Or you can simply get the most primitive judgment for no reason. what kind of thinking do you have not figured out?
Thanks


Reply to discussion (solution)

You run get_aa () on all the members of the array $ field ()
There will still be get_aa (), get_bb (), get_cc ()....
For the latter $ {'get _ '. $ v} (),

More general

$f = array(  'a' => function() { return 'A'; },  'b' => function() { return 'B'; },  'c' => function() { return 'C'; },  'd' => function() { return 'D'; },);$d = array('c', 'b');foreach(array_intersect_key($f, array_flip($d)) as $k=>$v) {  $r[$k] = $v();}print_r($r);
Array(    [b] => B    [c] => C)

You run get_aa () on all the members of the array $ field ()
There will still be get_aa (), get_bb (), get_cc ()....
For the latter $ {'get _ '. $ v} (),



Thanks for your reply

The program algorithms corresponding to each field are different and irregular.

Yes

$f = array(  'a' => function() { return 'A'; },  'b' => function() { return 'B'; },  'c' => function() { return 'C'; },  'd' => function() { return 'D'; },);$d = array('c', 'b');$r = array_intersect_key($f, array_flip($d));array_walk($r, function(&$v) { $v = $v(); });print_r($r);
Array(    [b] => B    [c] => C)

Yes

$f = array(  'a' => function() { return 'A'; },  'b' => function() { return 'B'; },  'c' => function() { return 'C'; },  'd' => function() { return 'D'; },);$d = array('c', 'b');$r = array_intersect_key($f, array_flip($d));array_walk($r, function(&$v) { $v = $v(); });print_r($r);
Array(    [b] => B    [c] => C)


Thanks for your advice
This array plays 6
If there is a problem, the array $ f in the actual application will be very large, and every value is an anonymous function, does it occupy memory? I think too much, you don't have to worry about it.

So do you think writing static functions one by one will not occupy the memory?

If you really need to dynamically execute static functions, writing anonymous functions is the best choice.
Not only can compilation overhead be reduced (if there are only 10 static functions, then 10 thousand are also compiled)
In addition, the function does not need to be named and is directly bound to the purpose, saving the corresponding check.

So do you think writing static functions one by one will not occupy the memory?

If you really need to dynamically execute static functions, writing anonymous functions is the best choice.
Not only can compilation overhead be reduced (if there are only 10 static functions, then 10 thousand are also compiled)
In addition, the function does not need to be named and is directly bound to the purpose, saving the corresponding check.



Thank you, boss,
That's it.

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.