无法在WordPress中重新声明函数错误

j2cgzkjk  于 12个月前  发布在  WordPress
关注(0)|答案(1)|浏览(176)

我有两个插件,其中我初始化sdk的数据记录。主要的问题是,每当我把sdk初始化代码在第二个插件,我得到一个以下错误:
PHP致命错误:在C:\Users\WPB\Local Sites\wpb-sdk-site\app\public\wp-content\plugins\related-posts-thumbnails\wpb-sdk\start.php中,无法重新声明wpb_dynamic_init()(之前在C:\Users\WPB\Local Sites\wpb-sdk-site\app\public\wp-content\plugins\sdk-test-plugin\wpb-sdk\start. php中声明:14)
我在两个插件中使用的代码是:插件一:sdk-test-plugin

if ( ! function_exists( 'stp_wpb' ) ) {
    // Create a helper function for easy SDK access.
    function stp_wpb() {
        global $stp_wpb;

        if ( ! isset( $stp_wpb ) ) {
            // Include  SDK.
            require_once dirname(__FILE__) . '/wpb-sdk/start.php';
            $stp_wpb = wpb_dynamic_init( array(
                    'id'    => 10,
                    'slug' => 'sdk-test-plugin',
                    'type' => 'plugin',
                    'public_key' => '7e240de74fb1ed08fa08d38063f6a691462a815',
                    'is_premium' => false,
                    'has_addons' => false,
                    'has_paid_plans' => false,
                    'menu' => array()
                ) );
        }

        return $stp_wpb;
    }

    // Init .
    stp_wpb();
    // Signal that SDK was initiated.
    do_action( 'stp_wpb_loaded' );
}

字符串

第二个插件:related-posts-thumbnails

if (!function_exists('rpt_wpb')) {
    // Create a helper function for easy SDK access.
    function rpt_wpb()
    {
        global $rpt_wpb;

        if (!isset($rpt_wpb)) {
            // Include  SDK.
            require_once dirname(__FILE__) . '/wpb-sdk/start.php';
            $rpt_wpb = wpb_dynamic_init(array(
                'id' => 7,
                'slug' => 'related-posts-thumbnails',
                'type' => 'plugin',
                'public_key' => '7e240de74fb1ed08fa08d38063f6a691462a815',
                'is_premium' => false,
                'has_addons' => false,
                'has_paid_plans' => false,
                'menu' => array()
            ));
        }

        return $rpt_wpb;
    }

    // Init .
    rpt_wpb();
    // Signal that SDK was initiated.
    do_action('rpt_wpb_loaded');
}

start.php文件(sdk文件)

require_once dirname(__FILE__) . '/require.php';
function wpb_dynamic_init($module)
{
    $wpb = Logger::instance($module['id'], $module['slug'], true);
    // $wpb->dynamic_init($module);
    return $wpb;
}

require.php文件

require_once dirname( __FILE__ ) . '/config.php';
require_once WPB_SDK_DIR_INCLUDES . '/class-wpb-sdk-logger.php';

和logger类(class-wpb-sdk-logger.php)

<?php

/**
 * Logger responsible for gathering user logs and send.
 * In effort to provide better customer experience.
 *
 * @since 2.0.1
 */

/**
 * Class responsible for logs data collection.
 */
class Logger
{
    private static $_instances = array();
    private $_module_id;
    private $_slug;
    /**
     * Class constructor.
     *
     * @return void
     */
    private function __construct($module_id, $slug = false, $is_init = false)
    {
        if ( ! $is_init && ! is_numeric($module_id) && ! is_string($slug)) {
            return false;
        }
        $this->_module_id = $module_id;
        $this->_slug = $slug;
        $this->hooks();
    }

    public static function instance($module_id, $slug = false, $is_init = false)
    {
        if (empty($module_id)) {
            return false;
        }
        if (!$is_init && true === $slug) {
            $is_init = true;
        }
        $key = 'm_' . $module_id;

        if (!isset(self::$_instances[$key])) {
            self::$_instances[$key] = new Logger($module_id, $slug, $is_init);
        }

        return self::$_instances[$key];
    }
}


现在让我知道如何解决这个问题,因为我已经创建了不同的示例,但我仍然得到问题无法重新声明wpb_dynamic_init()

t2a7ltrp

t2a7ltrp1#

我不知道start.php是如何包含的,但可能它包含了不止一次。
快速修复,在wpb_dynamic_init周围添加

if (!function_exists('wpb_dynamic_init')) {
    // your function
}

字符串
更好的解决方案,找到为什么start.php被多次包含。您可以在start.php中使用debug_backtrace来可视化调用堆栈

相关问题