1. 程式人生 > >WP_Widget_PostViews has a deprecated constructor 報錯解決

WP_Widget_PostViews has a deprecated constructor 報錯解決

今天在除錯 WordPress 的時候,打開了 DEBUG 資訊,結果額外出現瞭如下報錯資訊:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; WP_Widget_PostViews has a deprecated constructor in ***** /wp-content/plugins/wp-postviews/wp-postviews.php on line 670
Invalid comment status.

百度了一把,發現是函式和類構造名稱衝突了,解決辦法如下:

編輯 wp-postviews.php 這個檔案,定位到 670 行如下程式碼:

PHP
123456 classWP_Widget_PostViewsextendsWP_Widget{// ConstructorfunctionWP_Widget_PostViews(){$widget_ops=array('description'=>__('WP-PostViews views statistics','wp-postviews'));$this->WP_Widget('views',__('Views','wp-postviews'),$widget_ops);}

很明顯,function 名稱和 class 衝突了,所以我們只要把 function 名稱改成 __construct 即可:

PHP
123456 classWP_Widget_PostViewsextendsWP_Widget{// Constructorfunction__construct(){$widget_ops=array('description'=>__('WP-PostViews views statistics','wp-postviews'));$this->WP_Widget('views',__('Views','wp-postviews'),$widget_ops);}

儲存後這個錯誤就解決了,如果遇到類似的報錯,也可以參考解決了。