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)
Looking at the documentation located here it seems that only the name is required but the way PHP works your have to have the pre variables defined as well.
You have two ways of creating the class:
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
__construct
method to initialize your object, i would rewrite you code like so:The
WP_Widget::WP_Widget(..)
method is for PHP4 only, and should not be used in PHP 5 or higher.Now it seems that your using PHP 5.3 as your using name spaces so you can do the following:
It seems to me your problem is not in the namespaces, the following code works like a charm:
If you get an error message, it might be helpful to state what it says.
You want to call a parent method? Simply use
parent::MethodName();
If you want to call the parent constructor, use
parent::__construct()
– if your constructor is named like the class, rename it to__construct
which is the preferred name for a constructor for years…This worked fine for me:
You could also use PHP’s native
get_class
method, which will return any class instantiation’s name as a string.https://www.php.net/manual/en/function.get-class.php
register_widget(get_class(new UCIWordpressWidgetFooter()));
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 factory if it really is just a factory.
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.
But again, I have never used WordPress, it’s hard to tell without looking at the script.
You should post the exact error code here.