略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: get_defined_functions

2024-03-29

get_defined_functions

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

get_defined_functions返回所有已定义函数的数组

说明

get_defined_functions(bool $exclude_disabled = false): array

获取所有已定义函数的数组。

参数

exclude_disabled

禁用的函数是否应该在返回的数据里排除。

返回值

返回数组,包含了所有已定义的函数,包括内置(internal) 和用户定义的函数。 可通过$arr["internal"]来访问系统内置函数, 通过$arr["user"]来访问用户自定义函数 (参见示例)。

更新日志

版本 说明
PHP 7.0.15, PHP 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
        )

)

参见

add a noteadd a note

User Contributed Notes 11 notes

up
14
kkuczok at gmail dot com
9 years ago
You can list all arguments using ReflectionFunction class. It's not necessary to parse selected files/files as suggested by Nguyet.Duc.

http://php.net/manual/pl/class.reflectionfunction.php

Example:
<?php
function foo(&$bar, $big, $small = 1) {}
function
bar($foo) {}
function
noparams() {}
function
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[] = 'function ' . $func . ' ( ' . implode(', ', $args) . ' )' . PHP_EOL;
}
print_r($functions_list);
?>

Output:
Array
(
    [0] => function foo ( &&bar, &big, [$small = 1] )

    [1] => function bar ( &foo )

    [2] => function noparams (  )

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

)
up
7
peten at spam dot me dot not dot frontiernet dot net
20 years ago
Here's a useful trick with the get_defined_functions function - show all available functions with a link to the documentation (you can even change the mirror it goes to):

<?php
 
// the php mirror
 
$php_host = "http://us2.php.net/";

 
// the number of cols in our table
 
$num_cols = 3;

 
$ar = get_defined_functions();
 
$int_funct = $ar[internal];
 
sort($int_funct);
 
$count = count($int_funct);
?>
<html>
<head>
  <title>
   Available PHP Functions
  </title>
</head>
<body>
  <p>
   <?php print $count; ?> functions    
    available on
    <?php
     
print $_SERVER[SERVER_NAME];
    
?>
   (<a href="<?php print $php_host;?>"
    target="phpwin">php</a>
    version
    <?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>
up
8
mIHATESPAMduskis at bates dot edu
19 years ago
At least with PHP 4.2.3 on a GNU/Linux/Apache platform, get_defined_functions() returns user-defined functions as all-lower case strings regardless of how the functions are capitalized when they are defined.

Threw me for a loop.
up
6
strrev xc.noxeh@ellij
14 years ago
Please note that functions created with create_function() are not returned.
(However that might change in a later version)
up
5
berchentreff at berchentreff dot de
16 years ago
look at here, list all the defined function on your php-Version and give as well formatted output width links onto the php-manual:

<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>
up
2
Muneeb Aslam
6 years ago
We can also get the function names with this simple foreach

<?php
   
   
//Since its a two dimensional array
   
$functions = get_defined_functions();
    
   
//We can use nested foreach to get the function names
   
foreach($functions as $function){
         foreach(
$function as $thisFunc){
             echo
$thisFunc . "<br />";
         }
     }
   
?>
up
2
Nguyet.Duc
10 years ago
get_defined_functions() just returns a list of function names without function arguments. Here is the code to list user-defined function names plus arguments:

<?php
    $content
= file_get_contents('example.php');
   
preg_match_all("/(function )(\S*\(\S*\))/", $content, $matches);
    foreach(
$matches[2] as $match) {
       
$function[] = "// " . trim($match) . "<br />\n";
    }
   
natcasesort($function);
   
$functionlist .= "/* Functions in this file */<br />\n";
   
$functionlist .= "/**************************/<br />\n\n";
   
$functionlist .= implode('', $function);
    echo
$functionlist;
?>

Output:

/* Functions in this file */
/**************************/
// add_data($data)
// add_files($list)
// archive($name)
// bzip_file($name)
up
1
spudinski at gmail dot com
14 years ago
To search for a function.
<html>
<head>
<title>List of all Internal functions</title>
<style type="text/css">
body {
    background-color: #FFFFFF;
    color: #222222;
    font-size: 11px;
    font-family: arial, tahoma;
}
table {
    color: #222222;
    font-size: 11px;
    font-family: arial, tahoma;
}
tr.found {
    background-color: #66EE00;
    font-weight: bold;
}
a:link {
    color: #222222;
}
a:visited {
    color: #CCCCCC;
}
a:active {
    color: #444444;
}
a:hover {
    text-decoration: underline;
}
</style>
</head>
<body>
<p>
    <form method="GET">
    Search: <input type="text" name="search"><br>
    <input type="submit">
    </form>
</p>
<?php
   
if (!empty($_GET['search'])) {
        echo
'<p>' . '<a href="#' . $_GET['search'] . '">' .
       
'Goto ' . $_GET['search'] . '</a>' .
       
'<script type="text/javascript">
            window.onload = function() {
                document.location += "#'
. $_GET['search'] . '";
                return true;
            }
        </script>
        </p>'
;
    }
?>
<p>
    <table>
<?php
    $country
= 'us';
   
$functions = get_defined_functions();
   
$functions = $functions['internal'];
   
$num = 0;
    foreach(
$functions as $function) {
       
$num++;
        echo
'<tr ' . (($_GET['search'] == $function) ? 'class="found"' : '') . '><td>' .
       
number_format($num) . '</td><td>' . '<a name="' . $function . '" href="http://' . $country . '.php.net/' .
       
$function . '">' . $function . '</a>' . '</td></tr>';
    }
?>
    </table>
</p>
</body>
</html>
up
1
rob at webdimension dot co dot uk
11 years ago
A quick way of using this function:

<?php
// ALL USER DEFINED FUNCTIONS
$arr = get_defined_functions();
foreach (
$arr['user'] as $key => $value){
echo
$value.'<br />';
}
// ALL USER DEFINED FUNCTIONS

// ALL INTERNAL FUNCTIONS
$arr = get_defined_functions();
foreach (
$arr['internal'] as $key => $value){
echo
$value.'<br />';
}
// ALL INTERNAL FUNCTIONS
?>
up
2
Muneeb Aslam
6 years ago
This is rather a simple non-confusing script to get the function names linked to its manual page on php.net. Hope it helps someone. Commented script is self explainatory

<?php
   
   
/*declare a variable to php manual of functions.
    change the $lng to the region you want it for,
    i-e en/es/de etc etc */
   
$lng = "es";
   
$url = "http://www.php.net/manual/".$lng."/function.";
   
   
// get defined functions in a variable (it will be a 2D array)
   
$functions = get_defined_functions();
   
   
// Run nested foreach to get the function names
   
foreach($functions as $function){
        foreach (
$function as $functionName){
           
           
/* Since php manual is using hyphens instead of underscores
            for functions, we will convert underscores to hyphen whereever
            there is one. */
           
if(strpos($functionName,"_") !== false){
               
$functionForURL = str_replace("_","-",$functionName);
            } else {
               
$functionForURL = $functionName;
            }
           
           
/* echo the link */
           
echo "<a href='".$url.$functionForURL.".php'>".$functionName."</a><br />";
        }
    }
   
?>
up
0
ChaosKaizer
13 years ago
for user defined function

<?php
/**
* @param string $function_name The user function name, as a string.
* @return Returns TRUE if function_name  exists and is a function, FALSE otherwise.
*/
function user_func_exists($function_name = 'do_action') {
  
   
$func = get_defined_functions();
  
   
$user_func = array_flip($func['user']);
  
    unset(
$func);
  
    return ( isset(
$user_func[$function_name]) );  
}
?>

官方地址:https://www.php.net/manual/en/function.get-defined-functions.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3