wordpress 一共有多少个函数

学习wordpress 有一段时间了,一直比较好奇,wordpress 有多少个函数。wp-includes 是 WordPress 根目录下的核心系统目录,核心代码文件、通用工具函数、功能类库都在这个目录。就统计下 这个目录下的函数把。

<?php
/**
 * WordPress 函数统计 PHP 脚本(极简生效版)
 * 适配:所有PHP版本 + WordPress所有版本
 * 使用:php count-wp-functions.php
 */
set_time_limit(0);
ini_set('memory_limit', '512M');

// 初始化统计变量
$stats = [
    'file_count' => 0,
    'normal_functions' => 0,
    'class_methods' => 0,
    'total' => 0
];

// 只扫描 wp-includes 目录(已确认有844个PHP文件)
$dir = __DIR__ . '/wp-includes';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

foreach ($iterator as $file) {
    // 仅处理PHP文件
    if ($file->isFile() && $file->getExtension() === 'php') {
        $stats['file_count']++;
        $content = file_get_contents($file->getPathname());
        
        // 移除注释(避免误统计)
        $content = preg_replace('/\/\*.*?\*\//s', '', $content); // 移除多行注释
        $content = preg_replace('/\/\/.*?$/m', '', $content);    // 移除单行注释
        
        // 统计普通函数:匹配 function 函数名(
        preg_match_all('/^\s*function\s+[a-zA-Z0-9_]+\s*\(/m', $content, $func_matches);
        $stats['normal_functions'] += count($func_matches[0]);
        
        // 统计类方法:匹配 public/protected/private/static + function 方法名(
        preg_match_all('/^\s*(public|protected|private|static)\s+function\s+[a-zA-Z0-9_]+\s*\(/m', $content, $method_matches);
        $stats['class_methods'] += count($method_matches[0]);
    }
}

// 计算总数
$stats['total'] = $stats['normal_functions'] + $stats['class_methods'];

// 输出结果
echo "🔍 WordPress 函数统计结果\n";
echo "----------------------------\n";
echo "扫描PHP文件数:{$stats['file_count']} 个(预期844个)\n";
echo "普通函数数量:{$stats['normal_functions']} 个\n";
echo "类方法数量:{$stats['class_methods']} 个\n";
echo "总函数数(含方法):{$stats['total']} 个\n";
echo "----------------------------\n";
echo "💡 说明:已移除注释,统计结果为核心有效函数数\n";
?>
🔍 WordPress 函数统计结果
----------------------------
扫描PHP文件数:844 个(预期844个)
普通函数数量:3520 个
类方法数量:4002 个
总函数数(含方法):7522 个
----------------------------
💡 说明:已移除注释,统计结果为核心有效函数数

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注