Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 354
edelwater Avatar asked Mar 09 '11 14:03

edelwater


4 Answers

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:

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

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

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.

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

add_action('widgets_init', function() {
    return register_widget(new a\b\c\whatever);
});
like image 65
RobertPitt Avatar answered Nov 14 '22 08:11

RobertPitt


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.

like image 43
Berry Langerak Avatar answered Nov 14 '22 07:11

Berry Langerak


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...

like image 3
ThiefMaster Avatar answered Nov 14 '22 09:11

ThiefMaster


This worked fine for me:

add_action('widgets_init', function() {                 
    register_widget('\a\b\c\whatever');
});
like image 2
Pablo S G Pacheco Avatar answered Nov 14 '22 08:11

Pablo S G Pacheco