get_defined_functions
get_defined_functions(): array
返回进程函数表快照:internal(内建)与 user(脚本定义)两半——与 get_defined_vars 配对的内存自省。
C C23
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/* C 反向对照:「列出我有哪些函数」在 C 里的经典答案——
平行的函数指针表 + 名字表 */
static char buf[64] = {0}; /* 零初始化 */
static const char *greet(const char *w) {
snprintf(buf, sizeof buf, "hi %s", w);
return buf;
}
static const char *shout(const char *w) {
char tmp[64] = {0};
snprintf(tmp, sizeof tmp, "hi %s", w);
for (size_t i = 0; tmp[i]; i++) {
tmp[i] = (char)toupper((unsigned char)tmp[i]);
}
snprintf(buf, sizeof buf, "%s!", tmp);
return buf;
}
int main(void) {
const char *(*table[])(const char *) = {greet, shout};
const char *names[] = {"greet", "shout"};
printf("mine: %s,%s\n", names[0], names[1]);
printf("shout: %s\n", table[1]("php"));
return 0;
}
mine: greet,shout shout: HI PHP!
C++ C++26
#include <cctype>
#include <functional>
#include <iostream>
#include <map>
#include <string>
static std::string greet(const std::string &w) {
return "hi " + w;
}
static std::string shout(const std::string &w) {
std::string s = greet(w);
for (char &ch : s) {
ch = static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
}
return s + "!";
}
int main() {
// C++ 反向对照:std::function 映射表就是手写的函数表
std::map<std::string, std::function<std::string(const std::string &)>> table;
table["greet"] = greet;
table["shout"] = shout;
std::cout << "mine:";
for (const auto &entry : table) {
std::cout << " " << entry.first;
}
std::cout << "\n";
std::cout << "shout: " << table.at("shout")("php") << "\n";
return 0;
}
mine: greet shout shout: HI PHP!
C# v11.0.100
using System;
using System.Collections.Generic;
using System.Linq;
// C# 反向对照:没有运行时函数表——委托映射表顶上
string Greet(string w) => "hi " + w;
string Shout(string w) => Greet(w).ToUpper() + "!";
var table = new Dictionary<string, Func<string, string>> {
["greet"] = Greet,
["shout"] = Shout,
};
Console.WriteLine("mine: [" + string.Join(", ", table.Keys.OrderBy(k => k, StringComparer.Ordinal)) + "]");
Console.WriteLine("shout: " + table["shout"]("php"));
mine: [greet, shout] shout: HI PHP!
Go v1.27.4
package main
import (
"fmt"
"sort"
"strings"
)
// Go 反向对照:没有运行时函数表——真需要按名分发时把函数收进映射表
func greet(s string) string { return "hi " + s }
func shout(s string) string { return strings.ToUpper(greet(s)) + "!" }
func main() {
table := map[string]func(string) string{
"greet": greet,
"shout": shout,
}
names := []string{}
for name := range table {
names = append(names, name)
}
sort.Strings(names)
fmt.Println("mine:", names)
fmt.Println("shout:", table["shout"]("php"))
}
mine: [greet shout] shout: HI PHP!
Java v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.UnaryOperator;
public class Main {
static String greet(String w) { return "hi " + w; }
static String shout(String w) { return greet(w).toUpperCase() + "!"; }
public static void main(String[] args) {
// Java 反向对照:没有运行时函数表——需要按名分发时把方法引用
// 接进映射表
Map<String, UnaryOperator<String>> table = new LinkedHashMap<>();
table.put("greet", Main::greet);
table.put("shout", Main::shout);
System.out.println("mine: " + table.keySet());
System.out.println("shout: " + table.get("shout").apply("php"));
}
}
mine: [greet, shout] shout: HI PHP!
JavaScript v26.13.1
// JS 反向对照:没有可枚举的函数表——函数声明活在各自作用域里
// (非严格脚本里则在 globalThis 上)
function a44Greet(who) {
return "hi " + who;
}
function a44Shout(who) {
return a44Greet(who).toUpperCase() + "!";
}
console.log("declared:", typeof a44Greet, typeof a44Shout);
console.log("shout:", a44Shout("php"));
declared: function function shout: HI PHP!
PHP v8.6.0
// get_defined_functions:运行时函数表,切成 internal(内建)与
// user(脚本定义)两半——对本进程可调用函数的自省
function a44_greet(string $who): string { return "hi " . $who; }
function a44_shout(string $who): string { return strtoupper(a44_greet($who)) . "!"; }
$fns = get_defined_functions();
echo "keys: ", implode(",", array_keys($fns)), "\n";
echo "user functions: ", count($fns["user"]), "\n";
echo "mine registered: ",
var_export(in_array("a44_greet", $fns["user"], true), true), " ",
var_export(in_array("a44_shout", $fns["user"], true), true), "\n";
echo "strlen is internal: ", var_export(in_array("strlen", $fns["internal"], true), true), "\n";
echo a44_shout("php"), "\n";
keys: internal,user user functions: 2 mine registered: true true strlen is internal: true HI PHP!
Python v3.15.2
# Python 反向对照:没有内建函数表——inspect + globals()
# 让你自己重建用户定义的那一半
import inspect
def a44_greet(who):
return "hi " + who
def a44_shout(who):
return a44_greet(who).upper() + "!"
mine = sorted(name for name, obj in globals().items() if inspect.isfunction(obj))
print("mine:", mine)
print("shout:", a44_shout("php"))
mine: ['a44_greet', 'a44_shout'] shout: HI PHP!
Rust v1.99.1
// Rust 反向对照:没有运行时函数表——带名字的 fn 指针向量是
// 手写的替身
fn greet(w: &str) -> String {
format!("hi {}", w)
}
fn shout(w: &str) -> String {
format!("{}!", greet(w).to_uppercase())
}
fn main() {
let table: Vec<(&str, fn(&str) -> String)> = vec![("greet", greet), ("shout", shout)];
let names: Vec<&str> = table.iter().map(|(name, _)| name).copied().collect();
println!("mine: {:?}", names);
let shout_fn = table.iter().find(|(name, _)| *name == "shout").unwrap().1;
println!("shout: {}", shout_fn("php"));
}
mine: ["greet", "shout"] shout: HI PHP!
TypeScript v26.13.1
// TS 反向对照:声明在编译期就有类型;typeof 检查仍是运行时的故事
function a44Greet(who: string): string {
return "hi " + who;
}
function a44Shout(who: string): string {
return a44Greet(who).toUpperCase() + "!";
}
console.log("declared:", typeof a44Greet, typeof a44Shout);
console.log("shout:", a44Shout("php"));
declared: function function shout: HI PHP!
语言差异说明
C:符号表在编译期就定了——运行时只有函数指针表(示例);「列出已定义函数」是链接器的事(nm/objdump)。
C++:同 C——运行时无函数表;静态反射尚未落地,按名分发靠手写 map(示例)。
C#:运行时能枚举程序集里的方法(Assembly.GetTypes + GetMethods),但那是反射不是函数表;本地函数/委托的按名分发靠手写字典(示例)。
Go:运行时无函数表——runtime 包拿不到「所有已定义函数」;需要按名分发就手写 map[string]func(示例)。
Java:方法在 Class 对象里可枚举(getDeclaredMethods),但按名调用得走 Method 句柄或方法引用表(示例)——「自省有、函数指针无」的折中。
JavaScript:无可枚举函数表——函数是 first-class 值,散在各作用域/globalThis 里;typeof 检查(示例)是唯一的运行时确认。
PHP:get_defined_functions 返回进程函数表的完整快照:internal(几百个内建)与 user(脚本定义的,示例断言恰有 2 个)两半;与 get_defined_vars(A18,变量表)配对构成「内存自省」两件套。教学点:exclude_disabled 参数 8.5 起已弃用——禁用函数在编译期就移出函数表,参数已无效果。
Python:无内建表但 globals() + inspect.isfunction 能精确重建用户函数清单(示例);dir(module) 是模块视角的等价物。
Rust:函数表不存在——符号在编译期静态分发;fn 指针数组(示例)是「把函数当数据」的零成本形态。
TypeScript:同 JS;TS 的 typeof f === "function" 检查与编译期声明各管一头(示例)。