dongqindu8110 2011-03-09 14:36
浏览 79
已采纳

PHP命名空间5.3和WordPress小部件

I am using namespaces.

I try to create a WordPress widget (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?

(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)

  • 写回答

5条回答 默认 最新

  • doufusi2013 2011-03-09 14:42
    关注

    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
    ";
        }
    }
    
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题