Skip to content
Advertisement

PHP Namespace 5.3 and WordPress Widget

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 abc;
 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("abcwhatever");'));

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:xampphtdocswp2wp-includeswidgets.php on line 324 and defined in 
C:xampphtdocswp2wp-includeswidgets.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)

Advertisement

Answer

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 "$foon";
    }
}

namespace FooBar;

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

$foo = new FooBarFoo( );

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement