PHP 命名空间 5.3 和 WordPress 小部件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5247302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:44:23  来源:igfitidea点击:

PHP Namespace 5.3 and WordPress Widget

phpwordpressnamespaces

提问by edelwater

I am using namespaces.

我正在使用命名空间。

I try to create a WordPress widget (http://codex.wordpress.org/Widgets_API)

我尝试创建一个 WordPress 小部件(http://codex.wordpress.org/Widgets_API)

With namespaces the following gives an error because the arguments can not be passed (and without namespaces it obviously works like usual)

对于命名空间,以下给出了一个错误,因为无法传递参数(如果没有命名空间,它显然像往常一样工作)

 namespace a\b\c;
 class whatever extends \WP_Widget {
   function whatever() {
     parent::WP_Widget('name1', 'name2');
   }
 // .. other functions left out
 }
 add_action('widgets_init',
 create_function('', 'return register_widget("a\b\c\whatever");'));

uhm... what is the correct syntax for 'parent::WP_Widget' using namespaces?

嗯...使用命名空间的“parent::WP_Widget”的正确语法是什么?

(the COMPLETE error message is:

(完整的错误消息是:

Warning: Missing argument 2 for WP_Widget::__construct(), called in 
C:\xampp\htdocs\wp2\wp-includes\widgets.php on line 324 and defined in 
C:\xampp\htdocs\wp2\wp-includes\widgets.php on line 93

)

)

And the debugger shows nothing has been passed:

调试器显示没有通过:

Variables in local scope (#14)
$control_options = Undefined
$id_base = boolean false 
$name = Undefined
$widget_options =  Undefined

(only the $name is required)

(只有 $name 是必需的)

采纳答案by Berry Langerak

It seems to me your problem is not in the namespaces, the following code works like a charm:

在我看来,您的问题不在名称空间中,下面的代码就像一个魅力:

<?php
namespace Foo;

class Bar {
    function __construct( $foo ) {
        echo "$foo\n";
    }
}

namespace Foo\Bar;

class Foo extends \Foo\Bar {
    function __construct( ) {
        parent::__construct( "This should work." );
    }
}

$foo = new \Foo\Bar\Foo( );

If you get an error message, it might be helpful to state what it says.

如果您收到错误消息,说明其内容可能会有所帮助。

回答by RobertPitt

Looking at the documentation located hereit seems that only the name is required but the way PHP works your have to have the pre variables defined as well.

查看位于此处的文档,似乎只需要名称,但 PHP 的工作方式还必须定义预变量。

You have two ways of creating the class:

您有两种创建类的方法:

  • A:
    • WP_Widget __construct ([string $id_base = false], string $name, [array $widget_options = array()], [array $control_options = array()])
  • B:
    • WP_Widget WP_Widget ([ $id_base = false], $name, [ $widget_options = array()], [ $control_options = array()])
  • A:
    • WP_Widget __construct ([string $id_base = false], string $name, [array $widget_options = array()], [array $control_options = array()])
  • 乙:
    • WP_Widget WP_Widget ([ $id_base = false], $name, [ $widget_options = array()], [ $control_options = array()])

By preference you should always use the __constructmethod to initialize your object, i would rewrite you code like so:

根据偏好,您应该始终使用该__construct方法来初始化您的对象,我会像这样重写您的代码:

namespace a\b\c;

class whatever extends \WP_Widget
{
    function __construct()
    {
        parent::__construct('name1', 'name2');
    }

    /*
         * Other methods!
    */
}

The WP_Widget::WP_Widget(..)method is for PHP4 only, and should not be used in PHP 5 or higher.

WP_Widget::WP_Widget(..)方法仅适用于 PHP4,不应在 PHP 5 或更高版本中使用。

Now it seems that your using PHP 5.3 as your using name spaces so you can do the following:

现在看来您使用 PHP 5.3 作为您使用的名称空间,因此您可以执行以下操作:

add_action('widgets_init', function() {
    return register_widget(new a\b\c\whatever);
});

回答by ThiefMaster

You want to call a parent method? Simply use parent::MethodName();

你想调用父方法吗?只需使用parent::MethodName();

If you want to call the parent constructor, use parent::__construct()- if your constructor is named like the class, rename it to __constructwhich is the preferred name for a constructor for years...

如果您想调用父构造函数,请使用parent::__construct()- 如果您的构造函数的名称类似于类,则将__construct其重命名为多年来构造函数的首选名称...

回答by Pablo S G Pacheco

This worked fine for me:

这对我来说很好:

add_action('widgets_init', function() {                 
    register_widget('\a\b\c\whatever');
});

回答by cj5

You could also use PHP's native get_classmethod, which will return any class instantiation's name as a string.

您还可以使用 PHP 的本机get_class方法,它将以字符串形式返回任何类实例化的名称。

https://www.php.net/manual/en/function.get-class.php

https://www.php.net/manual/en/function.get-class.php

register_widget(get_class(new \UCI\Wordpress\Widget\Footer()));

register_widget(get_class(new \UCI\Wordpress\Widget\Footer()));

回答by Dmitri

I don't use Wordpress, but from looking at this it looks like its due to a very bad design of Wordpress. They seem to use static function WP_Widget which serves as a factory but shares the same name as class name. They should have called it factoryif it really is just a factory.

我不使用 Wordpress,但从这个角度来看,它看起来像是由于 Wordpress 的设计非常糟糕。他们似乎使用静态函数 WP_Widget 作为工厂,但与类名共享相同的名称。他们应该把它叫做工厂,如果它真的只是一个工厂。

Also, from what you posted in your comment, maybe you need to create a static function WP_Widget() in your subclass and then don't even call the parent. If the script really wants you to override the WP_Widget then this is what you should do.

此外,根据您在评论中发布的内容,您可能需要在子类中创建一个静态函数 WP_Widget(),然后甚至不调用父类。如果脚本真的希望您覆盖 WP_Widget,那么这就是您应该做的。

But again, I have never used Wordpress, it's hard to tell without looking at the script.

但同样,我从未使用过 Wordpress,不看脚本很难判断。

You should post the exact error code here.

您应该在此处发布确切的错误代码。