【发布时间】:2011-07-11 23:07:28
【问题描述】:
我正在使用命名空间。
我尝试创建一个 WordPress 小部件 (http://codex.wordpress.org/Widgets_API)
对于命名空间,以下给出错误,因为无法传递参数(并且没有命名空间,它显然像往常一样工作)
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");'));
嗯... 'parent::WP_Widget' 使用命名空间的正确语法是什么?
(COMPLETE 错误信息是:
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
)
并且调试器显示没有通过:
Variables in local scope (#14)
$control_options = Undefined
$id_base = boolean false
$name = Undefined
$widget_options = Undefined
(只需要 $name)
【问题讨论】:
-
在此处发布确切的错误消息。从代码来看,看起来并没有什么问题。另外,为什么你使用 parent 而不仅仅是 $this->WP_Widget?最后,WP_Widget 是 WP_Widget 类的构造函数吗?为什么函数与类同名?如果它是一个构造函数,那么最好使用 __construct()
-
否:有,自 2.8 以来,小部件工厂是 WordPress 的一部分:如果您在 Eclipse 中单击 WP_Widget,您会得到它的定义...core.svn.wordpress.org/trunk/wp-includes/widgets 它应该被覆盖而不是被_construct,看源码(忍不住没写)
-
@dmitri: parent::WP_Widget 和 $this->WP_Widget 都提供相同的错误,这两种变体都在网络上使用。 WP_Widget 是根据 CODEX 应该被覆盖的函数。
标签: php wordpress namespaces