插件踩坑经验:function_exists遗漏📅 2025-10-02  |  👁 4117 次浏览

DaoKeCMS插件开发中常见的function_exists守卫遗漏坑:

问题描述

模板直接调用插件函数不加function_exists守卫,插件停用后模板白屏。

错误写法

// 错误!无守卫
$cats = ng_get_cats();
foreach ($cats as $c) { /* ... */ }

正确写法

// 正确!有守卫
if (function_exists('ng_get_cats')) {
    $cats = ng_get_cats();
} else {
    $cats = [];
}

// 简写
$cats = function_exists('ng_get_cats') ? ng_get_cats() : [];
  • 模板调用插件函数必须function_exists守卫
  • 停用时返回空数组或空字符串
  • 确保模板在任何情况下都不白屏
  • 配合手动require_once插件main.php

芒阳网络团域cms开发中心

Copyright © 2023 刀客CMS内容管理系统