Parse error: syntax error, unexpected '{'

<?php
// ini_set('display_errors',0);
// error_reporting(E_ALL|E_STRICT);
class chimp_footer_menu extends WP_Widget {
    function construct() {
        //Create Widget
        parent::__construct() {
            'footer_menu_widget',
            __('The Footer menu Widget','text_domain'),
            array( 'description' => __('A Footer Menu Widget', 'text_domain' ))
        }
    }

    // Front End display
    public function widget($args, $instance) {}

    // Backend Form
    public function form() {}

    // Update Widget Values
    public function update() {}

}

add_action('widgets_init', function(){
    register_widget('chimp_footer_menu');
});
?>

Normally I believe that some '(' or ')' is incomplete and then such things happen, but I have counted them and everything seems to be ok.

Live site

parameters go between ( ), not between { }.

additionally, chimp_footer_menu::construct() must be called explicitly, at which point the parent constructor was already called (and thus likely caused an error).

1 Like

Fixing this doesnt fix the error →

    // Front End display
    public function widget($args, $instance) {}

    // Backend Form
    public function form($instance) {}

    // Update Widget Values
    public function update($new_instance, $old_instance) {}

Means?

By the way ui am learning from here.

wrong section of code. the issue is in the first method.

1 Like

the PHP class constructor method is named __construct(), not construct()

1 Like

I think "__" is used for language translation. You can check here in the default usage.

I think the issue is something else. I tried removing this, and that didn’t helped to fix the issue.

Please see this blog that __ is used here also.

nope, that’s _(). for __ see http://php.net/manual/en/language.oop5.magic.php

even your linked documentation does it correct. You should compare what you wrote with what is written there.

1 Like

Two opening brackets ( and just one closing ) before the opening curly bracket {

Tip: It would have helped to find it sooner if you stated which line.

1 Like

Line#7 →

That is the line that @Dormilich mentioned.

1 Like

Yupp, a function call cannot be followed by a block in the same statement. That’s what I try to explain the whole time.

1 Like

I think the problem is confusion about the underscore(s)

It is used by convention as a way to name private methods.

In WordPress it is used as shorthand for Gettext functions.

Your class has a method (function) named
“construct”
Within it you have
“parent::__construct”

There is no parent with that name.

1 Like

I think the opening and closing bracket nullifies each other →

add_action('widgets_init', function(){
    register_widget('chimp_footer_menu');
});

I have never seen a parent constructor used Iike that before. I’ve always thought an object call requires arguments to be passed within the parentheses. Not a curly brace after the parentheses. Also, isn’t a function call like that only in Javascript?

1 Like

Actually the problem is fixed.
First thing that I did is I deleted the {} from parent::__construct
Second thing I did is parent::__construct() was like this and the entire arguments was outside this. I put the entire argument into that.

Final version that is not generating the error →

    function construct() {
        //Create Widget
        parent::__construct(
            'footer_menu_widget',
            __('The Footer menu Widget','text_domain'),
            array( 'description' => __('A Footer Menu Widget', 'text_domain' ))
                                       );

    }

You mean localization, Right? I mean the language translations.

I didn’t understand this part though.

There we go. That’s how I’ve always seen it used. Never used like

parent::__construct() { }

I’m pretty sure () requires a semi-colon anyways since it’s an object call. So no semi-colon would render it with errors.

So the correct way is

parent::__construct();

or

parent::__construct(Arg argument);

or

parent::__construct($var);

I know that Javascript functions can be like

var Junk = function {
    // Junk here
};
1 Like

Can you please guide me over this since I have just started learning and know only a little bit of PHP. May be if there is some suggested link to read. Thanks!

Well, there’s really no documentations I know that tells that. But which one makes more sense and is correct?

$mysqli = new mysqli() {
    host, username, password, database
}

or

$mysqli = new mysqli(host, username, password, database);

When you want to pass an argument through an object or function, you have to inject it into the () in order for it to actually be passed.

1 Like

This One.