Rumah > pembangunan bahagian belakang > tutorial php > PHP通过字符串调用函数_PHP教程

PHP通过字符串调用函数_PHP教程

WBOY
Lepaskan: 2016-07-13 10:36:38
asal
832 orang telah melayarinya

1. call_user_func

<span>function</span> a<span>(</span><span>$b</span><span>,</span><span>$c</span><span>)</span><span>{</span>
		<span>echo</span> <span>$b</span><span>;</span>
		<span>echo</span> <span>$c</span><span>;</span>
<span>}</span>
<span>call_user_func</span><span>(</span><span>'a'</span><span>,</span> <span>"111"</span><span>,</span><span>"222"</span><span>)</span><span>;</span>
<span>call_user_func</span><span>(</span><span>'a'</span><span>,</span> <span>"333"</span><span>,</span><span>"444"</span><span>)</span><span>;</span>
 
<span>//显示 111 222 333 444</span>
<span>?></span>
 
<span>//调用类内部的方法比较奇怪,居然用的是array,不知道开发者是如何考虑的,当然省去了new,也是满有新意的:</span>
<span>class</span> a <span>{</span>
		<span>function</span> b<span>(</span><span>$c</span><span>)</span><span>{</span>
				<span>echo</span> <span>$c</span><span>;</span>
		<span>}</span>
<span>}</span>
<span>call_user_func</span><span>(</span><span>array</span><span>(</span><span>"a"</span><span>,</span> <span>"b"</span><span>)</span><span>,</span><span>"111"</span><span>)</span><span>;</span>
 
<span>//显示 111</span>
<span>?></span>
Salin selepas log masuk

2. call_user_func_array

call_user_func_array函数和call_user_func很相似,只不过是换了一种方式传递了参数,让参数的结构更清晰:

<span>function</span> a<span>(</span><span>$b</span><span>,</span> <span>$c</span><span>)</span><span>{</span>
		<span>echo</span> <span>$b</span><span>;</span>
		<span>echo</span> <span>$c</span><span>;</span>
<span>}</span>
<span>call_user_func_array</span><span>(</span><span>'a'</span><span>,</span> <span>array</span><span>(</span><span>"111"</span><span>,</span> <span>"222"</span><span>)</span><span>)</span><span>;</span>
 
<span>//显示 111 222</span>
<span>?></span>
 
<span>//call_user_func_array函数也可以调用类内部的方法的</span>
 
<span>Class</span> ClassA<span>{</span>
		<span>function</span> bc<span>(</span><span>$b</span><span>,</span> <span>$c</span><span>)</span> <span>{</span>
    		<span>$bc</span> <span>=</span> <span>$b</span> <span>+</span> <span>$c</span><span>;</span>
				<span>echo</span> <span>$bc</span><span>;</span>
		<span>}</span>
<span>}</span>
<span>call_user_func_array</span><span>(</span><span>array</span><span>(</span><span>'ClassA'</span><span>,</span><span>'bc'</span><span>)</span><span>,</span> <span>array</span><span>(</span><span>"111"</span><span>,</span> <span>"222"</span><span>)</span><span>)</span><span>;</span>
 
<span>//显示 333</span>
<span>?></span>
Salin selepas log masuk

call_user_func函数和call_user_func_array函数都支持引用,这让他们和普通的函数调用更趋于功能一致:

<span>function</span> a<span>(</span><span>&</span><span>$b</span><span>)</span><span>{</span>
		<span>$b</span><span>++;</span>
<span>}</span>
<span>$c</span> <span>=</span> <span>0</span><span>;</span>
<span>call_user_func</span><span>(</span><span>'a'</span><span>,</span> <span>&</span><span>$c</span><span>)</span><span>;</span>
<span>echo</span> <span>$c</span><span>;</span><span>//显示 1</span>
<span>call_user_func_array</span><span>(</span><span>'a'</span><span>,</span> <span>array</span><span>(</span><span>&</span><span>$c</span><span>)</span><span>)</span><span>;</span>
<span>echo</span> <span>$c</span><span>;</span><span>//显示 2</span>
Salin selepas log masuk

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/738521.htmlTechArticle1. call_user_func function a ( $b , $c ) { echo $b ; echo $c ; } call_user_func ( 'a' , "111" , "222" ) ; call_user_func ( 'a' , "333" , "444" ) ; //显示 111 222 333 444 ? //调用...
Label berkaitan:
sumber:php.cn
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
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan