3

我一直在我的 Wordpress 页面上实现某个插件(dtabs),但在升级到最新版本后,我发现我第二次调用名为dtab_list_tabs().

它的工作方式是,插件获得了 include_once'd,但无论您想在布局中放置选项卡多少次,都会调用 main 函数。我有 2 个这样的电话dtab_list_tabs()

现在,问题是,无论出于何种原因,开发人员决定直接在dtab_list_tabs()名为current_tab(). 因为它是在一个函数中声明的,显然 PHP 在您第二次调用父函数时会尝试重新声明它,这对我来说没有任何意义。

PHP 致命错误: 无法在第 1638 行的 .../wp-content/plugins/dtabs/dtabs.php 中重新声明 current_tab()(之前在 .../wp-content/plugins/dtabs/dtabs.php:1638 中声明)

该版本的代码位于http://plugins.svn.wordpress.org/!svn/bc/208481/dtabs/trunk/dtabs.php

我想弄清楚的是是否有办法告诉 PHP 是的……它有一个内部函数,据我所知,这是一个完全有效的 PHP 范例,所以不要重新声明它并失败。

至于手头的情况,我已经删除current_tab()了,因为它似乎没有被使用。

4

3 回答 3

5

您可以使用function_exists()来测试是否已经定义了具有该名称的函数。如果您使定义有条件( if(something) { function foo() {...} } )php 将仅在满足条件时“评估”定义。

function foo() {
  if ( !function_exists('bar') ) {
    function bar() {
      echo 'bar ';
    }
  }

  bar();
}

foo();
foo();

另见:http ://docs.php.net/functions.user-defined

(但我会尽量避免这样的事情)

于 2010-02-21T05:42:35.537 回答
3

您可以将函数声明包装在if语句中。用于function_exists()查看函数之前是否已声明。

if(!function_exists('current_tab')) {
  function current_tab() {
    myMagicCode();
  }
}
于 2010-02-21T05:43:48.290 回答
1

你可以试试这个:

if (!function_exists('my_function')) {
  function my_function() {

  }
}

function_exists()- 如果给定函数已定义,则返回 TRUE

于 2010-02-21T05:42:33.777 回答