php - Non-static method error when trying to use class in widgets

admin2025-01-07  5

I'm trying to call a method into a function widget. The $this of the widget is passed to that method, but I get the following error:

Strict Standards: Non-static method class_name::method_name() should not be called statically, assuming $this from incompatible context in C:\xampp\htdocs\wp\wp-content\themes\...... on line 446

Here's what I'm trying to do exactly.

The widget form function:

class new_widget extends WP_Widget {
    function form($instance) {
        <?php widget_fields::input($instance, 'title'); ?>
    }
}

And the class which I'm using out of the score of the widget class:

class widget_fields {
    function input($t, $slug) {
        ?>
        <p>
            <input type="text" name="<?php echo $this->get_field_name($slug); ?>" value="<?php echo $t[$slug]; ?>" />          
        </p>
        <?php

    }
}

I'm trying to call a method into a function widget. The $this of the widget is passed to that method, but I get the following error:

Strict Standards: Non-static method class_name::method_name() should not be called statically, assuming $this from incompatible context in C:\xampp\htdocs\wp\wp-content\themes\...... on line 446

Here's what I'm trying to do exactly.

The widget form function:

class new_widget extends WP_Widget {
    function form($instance) {
        <?php widget_fields::input($instance, 'title'); ?>
    }
}

And the class which I'm using out of the score of the widget class:

class widget_fields {
    function input($t, $slug) {
        ?>
        <p>
            <input type="text" name="<?php echo $this->get_field_name($slug); ?>" value="<?php echo $t[$slug]; ?>" />          
        </p>
        <?php

    }
}
Share Improve this question asked Mar 9, 2016 at 8:06 user1181378user1181378 291 silver badge8 bronze badges 2
  • My widget is fine. I just grabbed the part of function form($instance) from it. I wanted to show that I'm trying to add a form input through a class. The issue is just with using $this (of the widget) outside it. – user1181378 Commented Mar 9, 2016 at 8:28
  • Yeah. Know how to use $this out of scope of the widget class without getting an error? – user1181378 Commented Mar 9, 2016 at 10:00
Add a comment  | 

1 Answer 1

Reset to default 0

Like the error suggests, your are calling the function statically. If you wanna use it that way you gotta change

 function input

to:

static function input
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253799a153.html

最新回复(0)