该插件在激活过程中生成了 2651 个字符的意外输出,为什么?



我正在尝试开发一个插件。当我激活插件时,它向我显示以下错误消息:

该插件在以下过程中生成了 2651 个字符的意外输出 激活。如果您注意到"标头已发送"消息,则问题 如果存在联合 Feed 或其他问题,请尝试停用或移除 这个插件。

我不明白为什么它会显示此错误。我可以看到没有空格来显示此错误!

这是我的代码:

<?php
/*
Plugin Name:  Forum Roles
Plugin URI:   http://www.creativeartbd.com
Description:  Generate a google map
Version:      1.0
Author:       Shibbir
Author URI:   http://creativeartbd.com/bio/
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  gmape
Domain Path:  /languages/
*/
// custom forum roles and capabilites class
class BOJ_Forum_Roles {
function __construct() {
// register plugin activation hook
register_activation_hook( __FILE__,  'activation' );
// register plgin deactivation hook
register_deactivation_hook( __FILE__, 'deactivation' );
}
// plugin activation method
function activation () {
// get the default administrator tole
$role =& get_role('administrator');
// add forum capabilities to the administrator role
if( !empty($role) ) {
$role->add_cap('publish_forum_topics');
$role->add_cap('edit_others_forum_topics');
$role->add_cap('delete_forum_topics');
$role->add_cap('read_forum_topics');
}
// create the forum administator tole
add_role( 'forum_administrator', 'Forum Administrator', array(
'publish_forum_topics', 'edit_others_forum_topics', 'delete_forum_topics', 'read_forum_topics'
) );
// create the moderator role
add_role('forum_moderator', 'Forum Moderator', array(
'publish_forum_topics', 'edit_others_forum_topics','read_forum_topics'
));
// create the forum member role
add_role('forum_member', 'Forum Member', array(
'publish_forum_topics', 'read_forum_topics'
));
// create the forum suspended role
add_role( 'forum_suspended', 'Forum Suspeded', array(
'read_forum_topics'
) );
}
// plugin deactivation method
function deactivation () {
// get the default administrator role
$role =& get_role('administrator');
//remove forum capabilites to the administrator role
if(!empty($role)) {
$role->remove_cap('publish_forum_topics');
$role->remove_cap('edit_others_forum_topics');
$role->remove_cap('delete_forum_topics');
$role->remove_cap('read_forum_topics');
}
// set up an array of roles to delete
$roles_to_delete = array(
'forum_administrator',
'forum_moderator',
'forum_member',
'forum_suspended'
);
// loop through each role, deleting the role if necessary
foreach ($roles_to_delete as $role) {
// get the users of the role
$users = get_users(array(
'role'  =>  $role
));
// check if there are no users for the role
if( count($users) <= 0 ) {
//remove the role from the site
remove_role( $role );
}
}
}
}
$forum_roles = new BOJ_Forum_Roles();

你能告诉我如何解决它吗?

对于此错误

警告:call_user_func_array(( 期望参数 1 是有效的回调,找不到函数"激活"或函数名称无效。

你不想调用对象(类(的方法。为此,您可以将数组传递给回调,第一个元素是类名(在静态调用中(或对象的实例,第二个元素是方法名称,例如:

class BOJ_Forum_Roles {
public function __construct() {
register_activation_hook( __FILE__, [$this,'activation'] );
register_deactivation_hook( __FILE__, [$this,'deactivation'] );
}
public function activation(){...}
public function deactivation(){...}
}

在核心PHP中,call_user_func_array()是这样称呼的

call_user_func_array([$this,'activation'], $args);

http://php.net/manual/en/function.call-user-func-array.php

此错误的输出可能会为您提供看到的原始错误。

静态调用

只是为了完整起见,如果你有

public static function activation(){...}

你会这样称呼它(静态(

class BOJ_Forum_Roles {
public function __construct() {
register_activation_hook( __FILE__, [__CLASS__,'activation'] );
register_deactivation_hook( __FILE__, [__CLASS__,'deactivation'] );
}
public static function activation(){...}
public static function deactivation(){...}
}

使用__CLASS__常量比输入类名更可取,在 PHP5.5+ 中您也可以这样做self::class,尽管我相信常量更快一些。 我认为这同样适用于后期静态绑定static::class。 当然,您始终可以执行此操作(在 5.5+ 中(,BOJ_Forum_Roles::class在此示例中这没有什么意义,但它对命名空间很有用。

更新

展示。。。注意:在

$role =& get_role('administrator');

删除 Amp,因为您正在通过引用分配函数的结果。我不确定,但如果它是一个对象,它无论如何都会通过引用传递。 没关系,因为您可能不应该在您的函数中修改它,事实上这引起了我的注意(在停用中(:

$role =& get_role('administrator');
...
foreach ($roles_to_delete as $role) {
...
}

您正在循环中重新分配$role变量。 这可能会导致也可能不会导致问题(如果不测试我就无法确定(,但覆盖它可能是您一个简单的网站。 以这种方式覆盖变量(作为循环的赋值(通常是不好的做法,如果它以前已经声明过,因为这不是故意的事情。

干杯!

最新更新