[wp-trac] [WordPress Trac] #57887: The WordPress core does not support calling non-static methods for hook filters and actions within a namespace

WordPress Trac noreply at wordpress.org
Thu Mar 9 11:58:46 UTC 2023


#57887: The WordPress core does not support calling non-static methods for hook
filters and actions within a namespace
-------------------------------+-----------------------
 Reporter:  markcarbonell2013  |       Owner:  (none)
     Type:  defect (bug)       |      Status:  closed
 Priority:  normal             |   Milestone:
Component:  General            |     Version:
 Severity:  normal             |  Resolution:  invalid
 Keywords:                     |     Focuses:  rest-api
-------------------------------+-----------------------

Comment (by markcarbonell2013):

 Hey @TobiasBg @hellofromTonya @jrf and @knutsp. @jrf you were correct. I
 meant calling non-static methods. Sorry for the confusion in the bug's
 title. I just tested the snippet myself and it does not reproduce the bug
 correctly. I corrected it to illustrate my point appropriately.

 {{{
 namespace test;

 class MyPlugin {
   public function __construct() {
     add_action('foo', [__CLASS__ , 'do_something_static'], 10, 0);
     add_action('foo', [__CLASS__ , 'do_something_non_static'], 10, 0);
     add_action('foo', [$this, 'do_something_static'], 10, 0);
     add_action('foo', [$this, 'do_something_non_static'], 10, 0);
   }

   public function do_something_non_static() {
     error_log("If you read this, calling " . __FUNCTION__ . " worked");
   }

   public static function do_something_static() {
     error_log("If you read this, calling " . __FUNCTION__ . " worked");
   }
 }

 new MyPlugin();
 do_action('foo');
 }}}

 When I run it in WP Snippets I get the error notification depicted below:


 ----

 Don't Panic
 The code snippet you are trying to save produced a fatal error on line
 306:

 Non-static method test\MyPlugin::do_something_non_static() should not be
 called statically
 The previous version of the snippet is unchanged, and the rest of this
 site should be functioning normally as before.

 Please use the back button in your browser to return to the previous page
 and try to fix the code error. If you prefer, you can close this page and
 discard the changes you just made. No changes will be made to this site.


 ----

 In my error log I see:

 [09-Mar-2023 11:54:23 UTC] PHP Deprecated:  Non-static method
 test\MyPlugin::do_something_non_static() should not be called statically
 in /var/www/html/wp-includes/class-wp-hook.php on line 306
 [09-Mar-2023 11:54:23 UTC] If you read this, calling
 do_something_non_static worked
 [09-Mar-2023 11:54:23 UTC] If you read this, calling do_something_static
 worked
 [09-Mar-2023 11:54:23 UTC] If you read this, calling
 do_something_non_static worked


 ----

 The error is caused because calling {{{[__CLASS__,
 'do_something_non_static']}}} calls the method statically, even if the
 method is non-static. Is this expected behavior?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57887#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list