2024 PHP Conference Japan

get_defined_functions

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

get_defined_functions傳回所有已定義函式的陣列

說明

get_defined_functions(布林值 $exclude_disabled = true): 陣列

取得所有已定義函式的陣列。

參數

exclude_disabled

是否應從傳回值中排除已停用的函式。

傳回值

傳回一個多維陣列,其中包含所有已定義函式的清單,包括內建 (內部) 和使用者定義的函式。 可以透過 $arr["internal"] 存取內部函式,並使用 $arr["user"] 存取使用者定義的函式 (請參閱以下範例)。

更新日誌

版本 說明
8.0.0 參數 exclude_disabled 的預設值已從 false 改為 true
7.0.15, 7.1.1 新增了 exclude_disabled 參數。

範例

範例 #1 get_defined_functions() 範例

<?php
function myrow($id, $data)
{
return
"<tr><th>$id</th><td>$data</td></tr>\n";
}

$arr = get_defined_functions();

print_r($arr);
?>

上述範例將輸出類似以下的內容

Array
(
    [internal] => Array
        (
            [0] => zend_version
            [1] => func_num_args
            [2] => func_get_arg
            [3] => func_get_args
            [4] => strlen
            [5] => strcmp
            [6] => strncmp
            ...
            [750] => bcscale
            [751] => bccomp
        )

    [user] => Array
        (
            [0] => myrow
        )

)

參見

新增註解

使用者貢獻的註解 5 則註解

kkuczok at gmail dot com
11 年前
您可以使用 ReflectionFunction 類別列出所有參數。 不需要像 Nguyet.Duc 建議的那樣解析選定的檔案/檔案。

https://php.dev.org.tw/manual/pl/class.reflectionfunction.php

範例
<?php
函數 foo(&$bar, $big, $small = 1) {}
函數
bar($foo) {}
函數
noparams() {}
函數
byrefandopt(&$the = 'one') {}

$functions = get_defined_functions();
$functions_list = array();
foreach (
$functions['user'] as $func) {
$f = new ReflectionFunction($func);
$args = array();
foreach (
$f->getParameters() as $param) {
$tmparg = '';
if (
$param->isPassedByReference()) $tmparg = '&';
if (
$param->isOptional()) {
$tmparg = '[' . $tmparg . '$' . $param->getName() . ' = ' . $param->getDefaultValue() . ']';
} else {
$tmparg.= '&' . $param->getName();
}
$args[] = $tmparg;
unset (
$tmparg);
}
$functions_list[] = '函數 ' . $func . ' ( ' . implode(', ', $args) . ' )' . PHP_EOL;
}
print_r($functions_list);
?>

輸出
陣列
(
[0] => 函數 foo ( &&bar, &big, [$small = 1] )

[1] => 函數 bar ( &foo )

[2] => 函數 noparams ( )

[3] => 函數 byrefandopt ( [&$the = one] )

)
mIHATESPAMduskis at bates dot edu
22 年前
至少在 GNU/Linux/Apache 平台上的 PHP 4.2.3 版本中,`get_defined_functions()` 會將使用者自訂函式的名稱全部轉換為小寫字串,無論函式在定義時使用的是何種大小寫。

這讓我有點困惑。
peten at spam dot me dot not dot frontiernet dot net
22 年前
以下是一個使用 `get_defined_functions()` 函式的實用技巧 - 顯示所有可用的函式,並附上文件連結(您甚至可以更改連結指向的鏡像站點)

<?php
// PHP 鏡像站點
$php_host = "http://us2.php.net/";

// 表格的欄數
$num_cols = 3;

$ar = get_defined_functions();
$int_funct = $ar[internal];
sort($int_funct);
$count = count($int_funct);
?>
<html>
<head>
<title>
可用的 PHP 函式
</title>
</head>
<body>
<p>
<?php print $count; ?> 個函式
可在
<?php
print $_SERVER[SERVER_NAME];
?>
(<a href="<?php print $php_host;?>"
target="phpwin">php</a>
版本
<?php print phpversion(); ?>)
</p>
<table align="center" border="2">
<tr>
<?php
for($i=0;$i<$count;$i++) {
$doc = $php_host
. "manual/en/function."
. strtr($int_funct[$i], "_", "-")
.
".php";
print
" <td><a href=\"" . $doc
. "\" target=\"phpwin\">"
. $int_funct[$i]
.
"</a></td>\n";
if((
$i > 1)
&& ((
$i+$num_cols)%$num_cols==($num_cols-1)))
print
" </tr>\n <tr>\n";
}
for(
$i=($num_cols-($count%$num_cols));$i>0;$i--)
print
" <td>&nbsp;</td>\n";
?>
</table>
</body>
</html>
berchentreff at berchentreff dot de
18 年前
看看這裡,列出你的 PHP 版本中所有已定義的函式,並以良好的格式輸出,附帶 PHP 手冊的連結。

<html><head>
<style type="text/css"><!--
li{font-family:Verdana,Arail,sans-serif;width:500px;margin-top:7px;}
a{padding:4px;}
a.a1{font-size:12px;background-color:#CCCCCC;color:#663300;}
a.a1:hover{background-color:#663300;color:#CCCCCC;}
a.a1:visited{background-color:#fff;color:#999;}
a.a1:visited:hover{background-color:#fff;color:#999;}
a.a0{font-size:12px;background-color:#CCCCFF;color:#663399;}
a.a0:hover{background-color:#663399;color:#CCCCFF;}
a.a0:visited{background-color:#ffC;color:#999;}
a.a0:visited:hover{background-color:#ffC;color:#999;}
--></style>
</head><body style="background-color:#999;">
<?php
$arr
= get_defined_functions();

// 取得所有已定義函式
foreach(
$arr as $zeile){
// 迴圈處理內建函式和使用者定義函式
sort
($zeile); // 排序函式名稱
$s=0;
foreach(
$zeile as $bzeile){
// 迴圈處理每個函式名稱
$s=($s)?0:1; // 切換連結樣式
echo
"<li><a class='a".$s."' href='http://de.php.net/".$bzeile."'>".$bzeile."</a></li>";
}
}
?>
</body>
</html>
Muneeb Aslam
9 年前
這是一個相當簡單且易懂的腳本,用於獲取函式名稱並連結到 php.net 上的手冊頁面。希望它能幫助到一些人。程式碼中的註釋已進行說明。

<?php

/*宣告一個變數指向 PHP 函數手冊。
將 $lng 改成您想要的區域設定,
例如 en/es/de 等等 */
$lng = "es";
$url = "https://php.dev.org.tw/manual/".$lng."/function.";

// 將已定義的函數放入一個變數中(它會是一個二維陣列)
$functions = get_defined_functions();

// 使用巢狀 foreach 迴圈來取得函數名稱
foreach($functions as $function){
foreach (
$function as $functionName){

/* 由於 PHP 手冊使用連字號而不是底線
來表示函數,我們會將底線轉換為連字號。
*/
if(strpos($functionName,"_") !== false){
$functionForURL = str_replace("_","-",$functionName);
} else {
$functionForURL = $functionName;
}

/* 顯示連結 */
echo "<a href='".$url.$functionForURL.".php'>".$functionName."</a><br />";
}
}

?>
To Top