php call_user_func_array():参数#1($callback)必须是有效的回调[重复]

js81xvg6  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(392)

这个问题已经有答案了

Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback(1个答案)
关闭7天前。
我正在开发一个WordPress插件。我创建的主类激活和停用所有必要的功能,设置和插件的文件,我使用composer来管理类的包含。
昨天我把psr-4的主文件夹从src/改为admin/builder/,这就引起了问题。
现在项目的层次结构是这样的:

onfeed-facebook\
|---admin\
    |---builder
        |---Action\
        |---RSA\
        |---View\
        |---OnFeedMain.php
|---vendor\
|---assets\
|---onfeed.php
|---...

我应该指出,在编辑composer.json文件后,我还运行了composer dump-autoload命令...但我还是得到了这个错误:

Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, first array member is not a valid class name or object in C:\www\clientwp.local\wp-includes\class-wp-hook.php:310 
Stack trace: 
#0 C:\www\clientwp.local\wp-includes\class-wp-hook.php(334): WP_Hook->apply_filters('', Array) 
#1 C:\www\clientwp.local\wp-includes\plugin.php(517): WP_Hook->do_action(Array) 
#2 C:\www\clientwp.local\wp-admin\includes\plugin.php(816): do_action('deactivate_onfe...', false) 
#3 C:\www\clientwp.local\wp-admin\plugins.php(209): deactivate_plugins('onfeed-facebook...', false, false) 
#4 {main} thrown in C:\www\clientwp.local\wp-includes\class-wp-hook.php on line 310

下面是composer.json文件:

"autoload": {
        "psr-4": {
            "Oppimittinetworking\\OnfeedFacebook\\": "admin/builder/"
        }
    },
    "config": {
        "optimize-autoloader": true
    }

这是OnFeedMain.php类:

namespace Oppimittinetworking\OnfeedFacebook;
use Oppimittinetworking\OnfeedFacebook\Action\ONFActivate;
use Oppimittinetworking\OnfeedFacebook\Action\ONFDeactivate;
use Oppimittinetworking\OnfeedFacebook\RSA\ONFRSAEncrypt;
use Oppimittinetworking\OnfeedFacebook\RSA\ONFRSADecrypt;

class OnFeedMain {

    public function __construct() {
        ONFActivate::activate();
        ONFActivate::register_admin_scripts();
        ONFActivate::register_wp_scripts();
    }

    public function __deactivate() {
        ONFDeactivate::deactivate();
        ONFDeactivate::unregister_admin_scripts();
        ONFDeactivate::unregister_wp_scripts();
    }

    public static function encrypt_conn() { return new ONFRSAEncrypt(); }
    public function decrypt_data() { return new ONFRSADecrypt(); }
}

下面是onfeed.php文件:

require_once ONFEED_PLUGIN_PATH . '/vendor/autoload.php';
use Oppimittinetworking\OnfeedFacebook\OnFeedMain;

if ( class_exists( 'OnFeedMain' ) )
    $onfmain = new OnFeedMain();

// activaion hook
register_activation_hook( __FILE__, array( $onfmain, '__construct' ) );

// deactivation hook
register_deactivation_hook( __FILE__, array( $onfmain, '__deactivate' ) );

下面是ONFActivate.phpONFDeactivate.php类中的函数:
ONFActivate.php

namespace Oppimittinetworking\OnfeedFacebook\Action;

class ONFActivate {

    public static function activate() {
        // No relevant code ...
    }

    public static function register_admin_scripts() {
        add_action( 'admin_enqueue_scripts', array( 'Oppimittinetworking\OnfeedFacebook\Action\ONFActivate', "enqueue_admin" ) );

        add_action( 'admin_menu', array( "Oppimittinetworking\OnfeedFacebook\Action\ONFActivate", 'add_admin_pages' ) );
    }

    public static function add_admin_pages() {
        add_menu_page( 'OnFeed Facebook', 'OnFeed Facebook', 'manage_options', 'onfeed_admin_menu', array( 'Oppimittinetworking\OnfeedFacebook\Action\ONFActivate', 'admin_index' ), 'dashicons-facebook-alt', 110 );
    }

    public static function admin_index() {
        require_once plugin_dir_path( __FILE__ ) . '../../admin/builder/index.php';
    }

    public static function register_wp_scripts() {
        // TODO
    }

    public static function enqueue_admin() {
        // Enqueue admin css files
        // [email protected]
        wp_register_style( "bootstrap", "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" );
        wp_enqueue_style( "bootstrap" );

        // [email protected]
        wp_register_style( "font_awesome", "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" );
        wp_enqueue_style( "font_awesome" );

        wp_enqueue_style( "onfeed_main_css", plugins_url( "../../assets/css/main.css", __FILE__ ) );
        wp_enqueue_style( "onfeed_shortcut_css", plugins_url( "../../assets/css/shortcut.css", __FILE__ ) );
        wp_enqueue_style( "onfeed_feedspage_css", plugins_url( "../../assets/css/feedspage.css", __FILE__ ) );
        
        // Enqueue admin js files
        // [email protected]
        wp_enqueue_script( "jquery_3_7_1-min", plugins_url( "../../assets/js/jquery-3.7.1.min.js", __FILE__ ), null, '3.7.1', array( 'strategy' => 'async' ) );

        wp_enqueue_script( "onfeed_function_js", plugins_url( "../../assets/js/function.js", __FILE__ ), null, '2.2.0', array( 'strategy' => 'defer' ) );
        wp_enqueue_script( "onfeed_handshake_js", plugins_url( "../../assets/js/handshake.js", __FILE__ ), null, '2.2.0', array( 'strategy' => 'defer' ) );
        wp_enqueue_script( "onfeed_shortcut_js", plugins_url( "../../assets/js/shortcut.js", __FILE__ ), null, '2.2.0', array( 'strategy' => 'defer' ) );
        wp_enqueue_script( "onfeed_feedspage_js", plugins_url( "../../assets/js/feedspage.js", __FILE__ ), null, '2.2.0', array( 'strategy' => 'defer' ) );
    }

    public static function enqueue_wp() {
        // TODO
    }
}

ONFDeactivate.php

namespace Oppimittinetworking\OnfeedFacebook\Action;

class ONFDeactivate {

    public static function deactivate() {
        // Not relevant code ...
    }

    public static function unregister_admin_scripts() {
        add_action( 'admin_dequeue_scripts', array( 'Oppimittinetworking\OnfeedFacebook\Action\ONFDeactivate', "dequeue_admin" ) );
    }

    public static function unregister_wp_scripts() {
        // TODO
    }

    public static function dequeue_admin() {
        // Enqueue admin css files
        wp_dequeue_style( "onfeed_main_css" );
        wp_dequeue_style( "onfeed_shortcut_css" );
        wp_dequeue_style( "onfeed_feedspage_css" );

        // Enqueue admin js files
        wp_dequeue_script( "onfeed_main_js" );
        wp_dequeue_script( "onfeed_shortcut_js" );
        wp_dequeue_script( "onfeed_feedspage_js" );
    }

    public static function dequeue_wp() {
        // TODO
    }
}

我也想让你们都知道我已经检查了这个问题是否是由不正确的名称空间引起的,但是你们怎么能看到没有问题呢?
我也看了这个链接:

但问题依然存在
提前感谢您的回复。

jhkqcmku

jhkqcmku1#

我已经通过更新这一行解决了这个问题:If ( class_exists( 'OnFeedMain' ) )onfeed.php内部。
我忘了输入正确的命名空间:

If ( class_exists( 'OnFeedMain' ) )
    $onfmain = new OnFeedMain();

收件人:

If ( class_exists( 'Oppimittinetworking\OnfeedFacebook\OnFeedMain') )
    $onfmain = new OnFeedMain();

相关问题