PHP函數處理方法總結

call_user_func_array

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

call_user_func_array?—?調用回調函數,并把一個數組參數作為回調函數的參數

說明

mixed?call_user_func_array?(?callable?$callback?,?array?$param_arr?)

把第一個參數作為回調函數(callback)調用,把參數數組作(param_arr)為回調函數的的參數傳入。

參數

callback

被調用的回調函數。

param_arr

要被傳入回調函數的數組,這個數組得是索引數組。

返回值

返回回調函數的結果。如果出錯的話就返回FALSE

更新日志

版本說明
5.3.0對面向對象里面的關鍵字的解析有所增強。在此之前,使用兩個冒號來連接一個類和里面的一個方法,把它作為參數來作為回調函數的話,將會發出一個E_STRICT的警告,因為這個傳入的參數被視為靜態方法。

范例

Example #1?call_user_func_array()例子

<?php
function foobar($arg, $arg2) {echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {function bar($arg, $arg2) {echo __METHOD__, " got $arg and $arg2\n";}
}// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>

以上例程的輸出類似于:

foobar got one and two
foo::bar got three and four

Example #2?call_user_func_array()使用命名空間的情況

<?phpnamespace Foobar;class Foo {static public function test($name) {print "Hello {$name}!\n";}
}// As of PHP 5.3.0
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));// As of PHP 5.3.0
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));?>

以上例程的輸出類似于:

Hello Hannes!
Hello Philip!

Example #3 把完整的函數作為回調傳入call_user_func_array()

<?php$func = function($arg1, $arg2) {return $arg1 * $arg2;
};var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */?>

以上例程會輸出:

int(8)

Example #4 傳引用

<?phpfunction mega(&$a){$a = 55;echo "function mega \$a=$a\n";
}
$bar = 77;
call_user_func_array('mega',array(&$bar));
echo "global \$bar=$bar\n";?>

以上例程會輸出:

function mega $a=55
global $bar=55

call_user_func

(PHP 4, PHP 5, PHP 7)

call_user_func?—?把第一個參數作為回調函數調用

說明

mixed?call_user_func?(?callable?$callback?[,?mixed?$parameter?[,?mixed?$...?]] )

第一個參數?callback?是被調用的回調函數,其余參數是回調函數的參數。

參數

callback

將被調用的回調函數(callable)。

parameter

0個或以上的參數,被傳入回調函數。

Note:

請注意,傳入call_user_func()的參數不能為引用傳遞。

Example #1?call_user_func()?的參考例子

<?php
error_reporting(E_ALL);
function increment(&$var)
{$var++;
}$a = 0;
call_user_func('increment', $a);
echo $a."\n";call_user_func_array('increment', array(&$a)); // You can use this instead before PHP 5.3
echo $a."\n";
?>

以上例程會輸出:

0
1

返回值

返回回調函數的返回值。

更新日志

版本說明
5.3.0對面向對象里面的關鍵字的解析有所增強。在此之前,使用兩個冒號來連接一個類和里面的一個方法,把它作為參數來作為回調函數的話,將會發出一個E_STRICT的警告,因為這個傳入的參數被視為靜態方法。

范例

Example #2?call_user_func()?的例子

<?php
function barber($type)
{echo "You wanted a $type haircut, no problem\n";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
?>

以上例程會輸出:

You wanted a mushroom haircut, no problem
You wanted a shave haircut, no problem

Example #3?call_user_func()?命名空間的使用

<?phpnamespace Foobar;class Foo {static public function test() {print "Hello world!\n";}
}call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0?>

以上例程會輸出:

Hello world!
Hello world!

Example #4 用call_user_func()來調用一個類里面的方法

<?phpclass myclass {static function say_hello(){echo "Hello!\n";}
}$classname = "myclass";call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello'); // As of 5.2.3$myobject = new myclass();call_user_func(array($myobject, 'say_hello'));?>

以上例程會輸出:

Hello!
Hello!
Hello!

Example #5 把完整的函數作為回調傳入call_user_func()

<?php
call_user_func(function($arg) { print "[$arg]\n"; }, 'test'); /* As of PHP 5.3.0 */
?>

以上例程會輸出:

[test]

注釋

Note:

在函數中注冊有多個回調內容時(如使用?call_user_func()?與?call_user_func_array()),如在前一個回調中有未捕獲的異常,其后的將不再被調用。

create_function

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

create_function?—?Create an anonymous (lambda-style) function

說明

string?create_function?(?string?$args?,?string?$code?)

從傳遞的參數創建一個匿名函數,并返回一個唯一的名稱。

警告
此函數內部執行eval(),因此與eval()具有相同的安全性問題。 此外,它具有不良的性能和內存使用特性。
如果您使用的是PHP 5.3.0或更新版本,則應使用本機匿名函數。

參數

通常這些參數將作為單引號分隔的字符串傳遞。 使用單引號字符串的原因是保護變量名不被解析,否則,如果使用雙引號,則需要轉義變量名,例如。\$阿瓦爾。

args

函數參數。

code

功能碼。

返回值

以字符串形式返回唯一的函數名稱,或者返回錯誤的FALSE。

范例

Example #1 Creating an anonymous function with?create_function()

您可以使用此功能,(例如)從運行時收集的信息創建一個函數:

<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>

或者,可能有一般的處理函數可以將一組操作應用于參數列表:

Example #2 Making a general processing function with?create_function()

<?php
function process($var1, $var2, $farr)
{foreach ($farr as $f) {echo $f($var1, $var2) . "\n";}
}// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),create_function('$a,$b', $f1),create_function('$a,$b', $f2),create_function('$a,$b', $f3));echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);// now make a bunch of string processing functions
$garr = array(create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),create_function('$a,$b', '; return "CRCs: " . crc32($a) . ", ".crc32($b);'),create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";'));
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>

以上例程會輸出:

Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a)/b = 0.27122299212594Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282, 342550513
similar(a,b) = 11(45.833333333333%)

但是,對于lambda風格(匿名)函數來說,最常見的用法是創建回調函數,例如使用array_walk()或usort()

Example #3 Using anonymous functions as callback functions

<?php
$av = array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>

以上例程會輸出:

Array
([0] => the mango[1] => a mango[2] => that mango[3] => this mango
)

一串字符串從較短到較長的順序排列

<?php$sv = array("small", "larger", "a big string", "it is a string thing");
print_r($sv);?>

以上例程會輸出:

Array
([0] => small[1] => larger[2] => a big string[3] => it is a string thing
)

將其從更長到更短的排序

<?phpusort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);?>

以上例程會輸出:

Array
([0] => it is a string thing[1] => a big string[2] => larger[3] => small
)

forward_static_call_array

(PHP 5 >= 5.3.0, PHP 7)

forward_static_call_array?—?Call a static method and pass the arguments as array

說明

mixed?forward_static_call_array?(?callable?$function?,?array?$parameters?)

....

....

....

?

?

?

給個目錄

函數處理 函數

  • call_user_func_array?— 調用回調函數,并把一個數組參數作為回調函數的參數
  • call_user_func?— 把第一個參數作為回調函數調用
  • create_function?— Create an anonymous (lambda-style) function
  • forward_static_call_array?— Call a static method and pass the arguments as array
  • forward_static_call?— Call a static method
  • func_get_arg?— 返回參數列表的某一項
  • func_get_args?— 返回一個包含函數參數列表的數組
  • func_num_args?— Returns the number of arguments passed to the function
  • function_exists?— 如果給定的函數已經被定義就返回 TRUE
  • get_defined_functions?— 返回所有已定義函數的數組
  • register_shutdown_function?— 注冊一個會在php中止時執行的函數
  • register_tick_function?— Register a function for execution on each tick
  • unregister_tick_function?— De-register a function for execution on each tick

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/456274.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456274.shtml
英文地址,請注明出處:http://en.pswp.cn/news/456274.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Django刪除多對多表關系 :

刪除多對多表關系 &#xff1a; # 刪除子表與母表關聯關系,讓小虎不喜歡任何顏色 # 寫法1: child_obj Child.objects.get(name"apollo") colors_obj Colors.objects.all() child_obj.favor child_obj.save() # 寫法2: child_obj Child.objects.get(name"apo…

git push/pull時總需要輸入用戶名密碼的解決方案

在提交項目代碼或者拉代碼的時候&#xff0c;git會讓你輸入用戶名密碼&#xff0c;解決方案&#xff1a;&#xff08;我們公司用的是gitlab&#xff09;執行git config --global credential.helper store命令然后git push origin your-branch會讓你輸入用戶名和密碼&#xff0c…

Django源代碼寫DetailView與ListView

基于類的通用視圖 - 展平索引 通用顯示視圖 以下兩個通用的基于類的視圖旨在顯示數據。在許多項目中&#xff0c;它們通常是最常用的視圖。 一、DetailView django.views.generic.detail.DetailView 在執行此視圖時&#xff0c;self.object將包含視圖正在操作的對象。 此視圖…

開源商務智能軟件Pentaho

1 簡介Pentaho是世界上最流行的開源商務智能軟件,以工作流為核心的&#xff0c;強調面向解決方案而非工具組件的&#xff0c;基于java平臺的商業智能(Business Intelligence,BI)套件BI&#xff0c;之所以說是套件是因為它包括一個web server平臺和幾個工具軟件&#xff1a;報表…

chrome用type=file獲取圖片路徑并轉base64字符串

1 html頁面 <div class"col-sm-2" style"height: 200px;margin-top: 14px;"> <input id"photo" name" " type"file" value"選擇圖片" ng-model"photoUrl"> <input type"button&qu…

Python - Django - 中間件 process_exception

process_exception(self, request, exception) 函數有兩個參數&#xff0c;exception 是視圖函數異常產生的 Exception 對象 process_exception 函數的執行順序是按照 settings.py 中設置的中間件的順序的倒序執行 process_exception 函數只在視圖函數中出現異常的時候才執行…

NTV Media Server G3性能測試

Hello&#xff01;大家好&#xff0c;我是資深測試工程師Jackie&#xff0c;今天我來和大家一起對云視睿博的高性能流媒體服務器NTV Media Server G3做一次性能測試。今天測試有一個小目標&#xff0c;那就是驗證在一臺普通的PC機上&#xff0c;NTV Media Server G3的并發能力是…

人生不只是上坡路

從前的自己都是非常順利的&#xff0c;覺得自己有一天一定可以成就一番事業。 可是誰也預料不到這幾年的變化&#xff0c; 似乎人生就要跌落到了谷底&#xff0c; 不知道該如何去面對&#xff0c; 壓力很大、惶恐不安、不知道未來的路該如何去走。 人生不只是上坡路&#xff0c…

Django 時間與時區設置問題

Django 時間與時區設置問題 在Django的配置文件settings.py中&#xff0c;有兩個配置參數是跟時間與時區有關的&#xff0c;分別是TIME_ZONE和USE_TZ 如果USE_TZ設置為True時&#xff0c;Django會使用系統默認設置的時區&#xff0c;即America/Chicago&#xff0c;此時的TIME_…

Oracle+ASM單機環境下,開啟歸檔的最簡單的方法

在ASM單機環境下&#xff0c;開啟歸檔的最簡單的方法。環境&#xff1a;oracle11g 11.2.0.4 登陸sqlplus[oracleudevasm ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Thu Jun 8 18:20:41 2017Copyright (c) 1982, 2013, Oracle. All rights reserved…

【Python】 配置解析ConfigParser 命令行參數解析optparser

ConfigParser ConfigParser包裝了配置文件的讀取和寫入&#xff0c;使得python程序可以更加輕松操作配置文件了。這里的配置文件是指.ini的那種文件&#xff0c;基本格式如下 [section_a] a_key1 a_value1 a_key2 a_value2[section_b] b_key1 b_value1 b_key2 b_value2 b_k…

解決:build_attrs() takes at most 2 arguments (3 given)

1.這個原因是由于captcha版本安裝太低引起的&#xff0c;所以導致register頁面打開報錯 2.解決辦法就是安裝更高級版本的captcha 解決pip install django-simple-captcha0.5.5

docker registry v2與harbor的搭建

docker的倉庫 1 registry的安裝 docker的倉庫我們可以使用docker自帶的registry,安裝起來很簡單&#xff0c;但是可能有點使用起來不是很方便。沒有圖形化。 開始安裝 1 使用鏡像加速器 2 curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://dc945b6d.m…

在windows下安裝Redis

一、下載windows版本的Redis 由于官網上沒有windows版的下載地址&#xff0c;所以需要下載windows版本的Redis有以下兩個地址&#xff1a; 博主的csdn資源地址&#xff1a;http://download.csdn.net/detail/u010608551/9778240 github下載地址&#xff1a;https://github.com/M…

Django REST framework【學習內容】

快速入門 我們將創建一個簡單的允許管理員用戶查看和編輯系統中的用戶和組的API。 項目設置 創建一個名為 tutorial 的新django項目&#xff0c;然后啟動一個名為 quickstart 的新app。 # 創建項目目錄 mkdir tutorial cd tutorial# 創建一個virtualenv來隔離我們本地的包依…

DotNetCore跨平臺~發布腳本PowerShell的設計

回到目錄 這幾天對PS情有獨忠&#xff0c;被它的強大功能所希引&#xff0c;它可以快速部署&#xff0c;快速發布&#xff0c;將一些連帶的動作一次的完成&#xff0c;挺方便&#xff0c;類似于早期的bat文件&#xff0c;也像linux平臺的bash腳本&#xff0c;但功能上&#xff…

解決: 'Cannot call `.is_valid()` as no `data=` keyword argument was ' AssertionError: Cannot call `

#注冊 def add_person(request):p_name request.POST.get("p_name")p_password request.POST.get("p_password")person_data {"p_name": p_name,"p_password": p_password,}print(person_data)serializer PersonSerializer(person…

軟件與程序

一、Java的起源 最初是為家用電器設計的&#xff0c;因為其特點適合于internet&#xff0c; 于是通過internet成為一種計算語言&#xff0c;一個平臺&#xff0c;一個網絡計算的架構。 二、Java平臺分類 ①JavaSE適用于普通PC及筆記本電腦&#xff0c;為其他JAVA程序的開發和運…

Django使用n內置模塊發送HTML格式的郵件

def send(request):# subject "小伙子很帥"# message "不禁夸啊"# send_mail(subject,message,"18332191389163.com",["18332191389163.com"])# return HttpResponse("ok")from django.core.mail import EmailMultiAltern…

EDM營銷之如何使郵件列表更加有效

營銷轉化往往是營銷人員專攻的必修課&#xff0c;大數據時代&#xff0c;只有真實有效的活躍用戶數據&#xff0c;才能進一步促進轉化。但仍然有想走捷徑的企業會選擇通過購買來獲取用戶數據&#xff0c;不僅數據質量不高&#xff0c;還會降低自身信譽等級。本次Focussend從購買…