来自:http://ziming.org/archives/6695.html
?
1. call_user_func
<span style="color: #000000; font-weight: bold;">function</span> a<span style="color: #009900;">(</span><span style="color: #000088;">$b</span><span style="color: #339933;">,</span><span style="color: #000088;">$c</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span><span style="color: #990000;">call_user_func</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">"111"</span><span style="color: #339933;">,</span><span style="color: #0000ff;">"222"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #990000;">call_user_func</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">"333"</span><span style="color: #339933;">,</span><span style="color: #0000ff;">"444"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>?<span style="color: #666666; font-style: italic;">//显示 111 222 333 444</span><span style="color: #000000; font-weight: bold;">?></span>?<span style="color: #666666; font-style: italic;">//调用类内部的方法比较奇怪,居然用的是array,不知道开发者是如何考虑的,当然省去了new,也是满有新意的:</span><span style="color: #000000; font-weight: bold;">class</span> a <span style="color: #009900;">{</span> <span style="color: #000000; font-weight: bold;">function</span> b<span style="color: #009900;">(</span><span style="color: #000088;">$c</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span><span style="color: #009900;">}</span><span style="color: #990000;">call_user_func</span><span style="color: #009900;">(</span><span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"a"</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">"b"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #0000ff;">"111"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>?<span style="color: #666666; font-style: italic;">//显示 111</span><span style="color: #000000; font-weight: bold;">?></span>
Salin selepas log masuk
2. call_user_func_array
call_user_func_array函数和call_user_func很相似,只不过是换了一种方式传递了参数,让参数的结构更清晰:
<span style="color: #000000; font-weight: bold;">function</span> a<span style="color: #009900;">(</span><span style="color: #000088;">$b</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span><span style="color: #009900;">}</span><span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"111"</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">"222"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>?<span style="color: #666666; font-style: italic;">//显示 111 222</span><span style="color: #000000; font-weight: bold;">?></span>?<span style="color: #666666; font-style: italic;">//call_user_func_array函数也可以调用类内部的方法的</span>?<span style="color: #000000; font-weight: bold;">Class</span> ClassA<span style="color: #009900;">{</span> <span style="color: #000000; font-weight: bold;">function</span> bc<span style="color: #009900;">(</span><span style="color: #000088;">$b</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <span style="color: #000088;">$bc</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$bc</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span><span style="color: #009900;">}</span><span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">(</span><span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'ClassA'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'bc'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"111"</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">"222"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>?<span style="color: #666666; font-style: italic;">//显示 333</span><span style="color: #000000; font-weight: bold;">?></span>
Salin selepas log masuk
call_user_func函数和call_user_func_array函数都支持引用,这让他们和普通的函数调用更趋于功能一致:
<span style="color: #000000; font-weight: bold;">function</span> a<span style="color: #009900;">(</span><span style="color: #339933;">&</span><span style="color: #000088;">$b</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++;</span><span style="color: #009900;">}</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #990000;">call_user_func</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #339933;">&</span><span style="color: #000088;">$c</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//显示 1</span><span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #339933;">&</span><span style="color: #000088;">$c</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//显示 2</span>
Salin selepas log masuk
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31
Isu terkini
Menguruskan entiti "pengguna" antara agregat dalam DDD
Saya cuba menulis semula aplikasi monolitik pada aplikasi monolitik modular menggunakan DD...
daripada 2024-04-04 09:54:15
0
1
361
Topik-topik yang berkaitan
Lagi>
- Apakah yang berlaku apabila alamat ip tidak tersedia?
- Bagaimana untuk belajar bahasa go dari 0 asas
- nombor siri dan kunci cad2012
- Perbezaan antara rantau semasa dan julat terpakai
- Apakah perbezaan antara mesin demo dan mesin sebenar?
- Apakah perisian tayangan perdana
- ralat permulaan mom.exe
- Apakah kod ruang dalam html