Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 关于php中选择性声明变量

关于php中选择性声明变量

Jun 23, 2016 pm 01:02 PM

我也不知道有木有这种东西,先看看我的过程
最开始:

$field = ['aa', 'bb', .......];#十几二十之多$data = array();foreach($field as $v){            switch($v){            case "aa": $data[$v] = get_aa();break;            .......            }} 
Copy after login

后来说
如果$field只有一个值zz
这样程序会从第一个case一个一个比对判断下去,不好
然后我就
 $field = ['aa', 'bb', .......];#十几二十之多 $aa = function(){return get_aa();}; #将运算代码放入匿名函数 $data = array(); foreach($field as $v){                $data[$v] = $$v(); }
Copy after login

#这样就免去很多判断了,问题来了
#这样的话需要事先声明很多$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);
Copy after login
Array(    [b] => B    [c] => C)
Copy after login
Copy after login
Copy after login

你对数组 $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);
Copy after login
Copy after login
Array(    [b] => B    [c] => C)
Copy after login
Copy after login
Copy after login

还可以

$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);
Copy after login
Copy after login
Array(    [b] => B    [c] => C)
Copy after login
Copy after login
Copy after login


感谢指点
这数组玩的6
有个问题,那实际应用中数组$f会特别大了,而且每个值都是匿名函数这东西,是不是特占内存,还是说我想多了,其实不用考虑的

那么你认为书写一个一个的静态函数就不占内存了吗?

如果真的需要动态执行静态函数的话,那么写成匿名函数就是最佳的选择
不但可以减少编译开销(如果1万个静态函数,只用到十个,那9990个也是要编译的)
而且函数不用命名,直接与用途绑定,省去了对应检查

那么你认为书写一个一个的静态函数就不占内存了吗?

如果真的需要动态执行静态函数的话,那么写成匿名函数就是最佳的选择
不但可以减少编译开销(如果1万个静态函数,只用到十个,那9990个也是要编译的)
而且函数不用命名,直接与用途绑定,省去了对应检查



感谢老大,
就这么搞了
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

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

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

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�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

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? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

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

See all articles