关于php中选择性声明变量
我也不知道有木有这种东西,先看看我的过程
最开始:
$field = ['aa', 'bb', .......];#十几二十之多$data = array();foreach($field as $v){ switch($v){ case "aa": $data[$v] = get_aa();break; ....... }}
后来说
如果$field只有一个值zz
这样程序会从第一个case一个一个比对判断下去,不好
然后我就
$field = ['aa', 'bb', .......];#十几二十之多 $aa = function(){return get_aa();}; #将运算代码放入匿名函数 $data = array(); foreach($field as $v){ $data[$v] = $$v(); }
#这样就免去很多判断了,问题来了
#这样的话需要事先声明很多$aa、$bb这样的变量,所以我就琢磨如果$field=['zz'];能不能选择性只加载$zz这个变量
或者说直接将最原始的无故判断给搞定了去,有啥子思路咱没琢磨到的
感谢
回复讨论(解决方案)
你对数组 $field 的成员都执行 get_aa()
还是会有 get_aa()、get_bb()、get_cc() ....
如果是后者 ${'get_'. $v}() 就可以了
更一般的
$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)
你对数组 $field 的成员都执行 get_aa()
还是会有 get_aa()、get_bb()、get_cc() ....
如果是后者 ${'get_'. $v}() 就可以了
感谢回复
每个字段对应的程序算法都是不一样的,毫无规律的
还可以
$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)
还可以
$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)
感谢指点
这数组玩的6
有个问题,那实际应用中数组$f会特别大了,而且每个值都是匿名函数这东西,是不是特占内存,还是说我想多了,其实不用考虑的
那么你认为书写一个一个的静态函数就不占内存了吗?
如果真的需要动态执行静态函数的话,那么写成匿名函数就是最佳的选择
不但可以减少编译开销(如果1万个静态函数,只用到十个,那9990个也是要编译的)
而且函数不用命名,直接与用途绑定,省去了对应检查
那么你认为书写一个一个的静态函数就不占内存了吗?
如果真的需要动态执行静态函数的话,那么写成匿名函数就是最佳的选择
不但可以减少编译开销(如果1万个静态函数,只用到十个,那9990个也是要编译的)
而且函数不用命名,直接与用途绑定,省去了对应检查
感谢老大,
就这么搞了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
