関数 get_defined_functions

関数 get_defined_functions は、定義済みのすべての関数のリストを含む多次元配列を返します。 配列には2つのキーがあります: PHPの組み込み関数用の 'internal' とユーザー定義関数用の 'user' です。 この関数はパラメータを受け取りません。

構文

phpget_defined_functions();

例

定義済みのすべての関数のリストを取得します:

php<?php
  function customFunction() {}
  $res = get_defined_functions();
  print_r(array_slice($res['internal'], 0, 3));
  print_r($res['user']);
?>

コードの実行結果 (例):

php[
  'zend_version',
  'func_num_args',
  'func_get_args'
]
['customFunction']

例

特定の関数の存在を確認します:

php<?php
  $functions = get_defined_functions();
  if (in_array('strpos', $functions['internal'])) {
    echo 'Function strpos exists';
  }
?>

コードの実行結果:

php'Function strpos exists'

例

ユーザー定義関数の数を数えます:

php<?php
  function func1() {}
  function func2() {}
  $res = get_defined_functions();
  echo 'User functions count: ' . count($res['user']);
?>

コードの実行結果:

php'User functions count: 2'

関連項目