找不到类,但它在文件中可用,我得到:致命错误:未捕获错误:找不到类‘BLOCKS\\Base\\Activation’

时间:2020-11-24 作者:Mähnenwolf

Class cannot be found, but the declaration is correct. Here is my code:

Info-Wheel.php

if ( file_exists( dirname( __FILE__ ) . "/vendor/autoload.php" ) ) {
  require_once dirname( __FILE__ ) . "/vendor/autoload.php";
}


use Blocks\\Base\\Activation;
use Blocks\\Base\\Deactivation;
/**
 * The code that runs during plugin activation
 */
function iw_activate_plugin() {
>>  Activation::activate();
}
/**
 * The code that runs during plugin deactivation
 */
function iw_deactivate_plugin() {
  Deactivation::deactivate();
}
/**
 * Register Hooks for deactivation and activation
 */
register_activation_hook( __FILE__, "iw_activate_plugin" );
register_deactivation_hook( __FILE__, "iw_deactivate_plugin" );
/**
 * If class exists run it
 */
if ( class_exists( "Inc\\\\Init" ) ) {
  Blocks\\Init::register_services();
}
Activation.php

 /**
  * Remove rewrite rules and then recreate rewrite rules.
  */
 class Activation {
  public static function activate() {
    flush_rewrite_rules(  );
   }
 }

I use composer, so I can use namespaces. Here is an image of my folder structure:
Folder Structure

In my opinion, the class Activation should be found and executed, but somehow WordPress can\'t find it? I have checked the names and copy-pasted them, just to be sure they\'re the same. I marked line 56 with a double arrow.

Here is the full error message:


Fatal error: Uncaught Error: Class \'Blocks\\Base\\Activation\' not found in D:\\Programme\\MAMP\\htdocs\\wordpress\\wp-content\\plugins\\Info-Wheel\\Info-Wheel.php:56
Stack trace:
#0 D:\\Programme\\MAMP\\htdocs\\wordpress\\wp-includes\\class-wp-hook.php(287): iw_activate_plugin(\'\')
#1 D:\\Programme\\MAMP\\htdocs\\wordpress\\wp-includes\\class-wp-hook.php(311): WP_Hook->apply_filters(\'\', Array)
#2 D:\\Programme\\MAMP\\htdocs\\wordpress\\wp-includes\\plugin.php(478): WP_Hook->do_action(Array)
#3 D:\\Programme\\MAMP\\htdocs\\wordpress\\wp-admin\\plugins.php(193): do_action(\'activate_Info-W...\')
#4 {main} thrown in D:\\Programme\\MAMP\\htdocs\\wordpress\\wp-content\\plugins\\Info-Wheel\\Info-Wheel.php on line 56

Edit: Here is a line from the autoloader:

"autoload": {
      "psr-4": {"Blocks\\\\": "./Blocks"}
  }
1 个回复
SO网友:Mähnenwolf

这是composer的缓存问题。

我追溯更改了composer.json, 所以我必须打开终端然后运行composer dumpautoload 更正缓存。