WordPress

【PHPエラー/解決済】Methods with the same name as their class will not be constructors in

WordPressのオリジナルウィジェットを作っているときに、『Methods with the same name as their class will not be constructors in a future version of PHP〜』というエラーが出てきました。

状況としては、以前のPHPのバージョンから7.xにしたときに発生。
新しいパソコンにしたときに、アプリなどを再インストールしたので、自動的にバージョンアップしてしまったようです。

エラー時のソースコード

class my_widget extends WP_Widget {
 function my_widget() {
   parent::__construct(
     false,

     <省略>

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

●エラー文
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; my_widget has a deprecated constructor in〜

「クラス名と同じメソッド名をつけないでね」ということみたいです。
以下のサイトを参考にして、ソースコードを修正しました。

ソースコードを修正

class my_widget extends WP_Widget {
 function __construct() { //ここを書き換える(『my_widget』→『__construct』)
   parent::__construct(
     false,

     <省略>

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

COMMENT

メールアドレスが公開されることはありません。 が付いている欄は必須項目です