Home » Php » php – How do I replace the deprecated "constructor" method for PHP7 in WordPress?

php – How do I replace the deprecated "constructor" method for PHP7 in WordPress?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m working on moving a site from a PHP5 server to a server with PHP7. My client is using a theme from the company Themelovin which appears to have gone dark. They have a few helper plugins like a Portfolio plugin, Twitter plugin and Teams plugin that come from a private repo with the purchase of a theme. I tried to get the latest version to see if it’s been fixed but it hasn’t.

Their Portfolio and Teams plugins both used an old constructor that is now deprecated with PHP7.

The original code looked like so:

 class thmlv_widget_team extends WP_Widget{
   function thmlv_widget_team(){
     $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
     $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
     $this->WP_Widget('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
   }

From what i’ve read, the issue here is the class “thmlv_widget_team” is the same name as the function. I am very poor at PHP but my understanding is that is the conflict. I read that you can fix this using __construct() and creating something that uses that plus a backup however that still isn’t working. This is what I have now:

class thmlv_widget_team extends WP_Widget{
    function __construct(){
      $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
      $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
      $this->WP_Widget('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
    }

    function thmlv_widget_team(){
      self::__construct();
    }

Ultimately I created the function __construct() with the code and then had a fallback for the original function to reference the construct I created.

Can you assist in what i’m missing here? The error i’m getting is as follows:

Notice: The called constructor method for WP_Widget in thmlv_widget_team is deprecated since version 4.3.0! Use __construct() instead. in /wordpress/wp-includes/functions.php on line 4716

I don’t know why it’s referencing the functions.php file but i’m updated to the latest version of WordPress (5.3.2).

This is the WordPress Theme in use.

https://themeforest.net/item/tag-creative-agencyportfolio-fullscreen-theme/9294146

How to&Answers:

The error message is pretty clear: remove function thmlv_widget_team(). This is a constructor that was neccessary in PHP 4, but has been deprecated with the introduction of PHP 7

Answer:

Hi from a quick look i think that this

class thmlv_widget_team extends WP_Widget{
   function thmlv_widget_team(){
     $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
     $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
     $this->WP_Widget('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
   }

shoud be this

class thmlv_widget_team extends WP_Widget{
   function thmlv_widget_team(){
     $widget_ops = array('classname' => 'team-items', 'description' => __('Display team members.', 'themelovin'));
     $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'thmlv_widget_team');
     parent::__construct('thmlv_widget_team', __('Themelovin Team', 'themelovin'), $widget_ops, $control_ops);
   }

more info https://mainwp.com/notice-the-called-constructor-method-for-wp_widget-is-deprecated-since-version-4-3-0-use-__construct/