Link to home
Start Free TrialLog in
Avatar of Emilio Lopez
Emilio Lopez

asked on

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP

Hi experts, i'm getting some errors or warnings on my Web page since the hosting company upgrade the version of php. I understand that the code needs to be changed but I'm lost.

can you guys help me?

thank you in advance.

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; g_twitter_feed has a deprecated constructor in /nas/content/live/bdgalardo/wp-content/plugins/shortcodesdex/includes/widgets/twitter-feed.php on line 9

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; generosity_Recent_Post_Thumbnail has a deprecated constructor in /nas/content/live/bdgalardo/wp-content/themes/generosity/widgets/recent-post.php on line 9

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; generosity_Popular_Post_Thumbnail has a deprecated constructor in /nas/content/live/bdgalardo/wp-content/themes/generosity/widgets/popular-post.php on line 9

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; generosity_Slider_Gallery has a deprecated constructor in /nas/content/live/bdgalardo/wp-content/themes/generosity/widgets/gallery-slider.php on line 9

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; generosity_Tags_Cloud_C has a deprecated constructor in /nas/content/live/bdgalardo/wp-content/themes/generosity/widgets/tags-cloud-number.php on line 9

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /nas/content/live/bdgalardo/wp-content/plugins/js_composer/include/classes/core/class-vc-mapper.php on line 111
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Methods with the same name as their class will not be constructors in a future version of PHP;
you got to rename the method to a different name than the class name.

Deprecated features in PHP 7.0.x
https://www.php.net/manual/en/migration70.deprecated.php
Sure. So in older versions of PHP, when class constructors were functions that just had the same name as the class, like this:
class Automobile
{
  public function Automobile()
  {
    echo "A new automobile has been created!";
  }
}

class Fruit
{
  public function Fruit()
  {
    echo "Yum!";
  }
}

Open in new window


So when I created/constructed new instances of these classes, it would look for the methods with the same names as the classes (so when creating new instances, the Automobile class would run the Automobile() method and the Fruit class would run the Fruit() method):
<?php
$toyota = new Automobile(); // This echoes "A new automobile has been created!"
$watermelon = new Fruit(); // This echoes "Yum!"
?>

Open in new window


In newer versions of PHP, instead of the method's name being the same as the class, you just use the method name __construct():
class Automobile
{
  public function __construct()
  {
    echo "A new automobile has been created!";
  }
}

class Fruit
{
  public function __construct()
  {
    echo "Yum!";
  }
}

Open in new window


That's usually all there is to it. The usage stays the same:
<?php
$toyota = new Automobile(); // This echoes "A new automobile has been created!"
$watermelon = new Fruit(); // This echoes "Yum!"
?>

Open in new window

Avatar of Emilio Lopez
Emilio Lopez

ASKER

Thank you so much guys for your help.

I'm very sorry to come back until now

So, in the example below the error is found on line 7 that contains this: class g_twitter_feed extends WP_Widget {

As I understand I need to change the line 8 than contains this: function g_twitter_feed()

And change it for this: function __construct()

Thank you again

  1. <?php
  2. add_action('widgets_init', 'g_twitter_feed_widgets');
  3. function g_twitter_feed_widgets()
  4. {
  5.      register_widget('g_twitter_feed');
  6. }
  7. class g_twitter_feed extends WP_Widget {
  8.      function g_twitter_feed()
  9.      {
  10.            $widget_ops = array('classname' => 'widget-tweets', 'description' => 'Slideshow of twitter feed.');
  11.            $control_ops = array('id_base' => 'twitter_feed-widget');
  12.            parent::__construct('twitter_feed-widget' , __('(theme)Twitter Feed Slider','generosity'),$widget_ops,$control_ops);
  13.      }
  14.      function widget($args, $instance)
  15.      {
  16.            extract($args);
  17.            $title = apply_filters('widget_title', $instance['title']);
  18.            $number = esc_attr($instance['number']);
  19.            $username = esc_attr($instance['username']);
  20.            echo wp_kses_post($before_widget);
  21.            if($args['id']=='footer-1'||$args['id']=='footer-2'||$args['id']=='footer-3'||$args['id']=='footer-4'):
  22.                  if($title) {
  23.                        echo wp_kses_post($before_title) . $title . $after_title;
  24.                  }
  25.                  ?>
  26.                  <!-- tweet Slider -->
  27.                  <div class="tweets-slider" data-username="<?php echo esc_attr($username) ?>" data-count="<?php echo esc_attr($number) ?>">
  28.            </div>
  29.            <?php else:
  30.                  if($title) {
  31.                        echo wp_kses_post($before_title) . $title . $after_title;
  32.                  }
  33.                  ?>
  34.                  <span class="float-username"><a href="https://twitter.com/<?php echo esc_attr($username) ?>">@<?php echo esc_attr($username) ?></a> <i class="fa fa-twitter"></i></span>
  35.                  <!-- tweet Slider -->
  36.                  <div class="tweet feeds" data-username="<?php echo esc_attr($username) ?>" data-count="<?php echo esc_attr($number) ?>"></div>
  37.            <?php
  38.            endif;
  39.            echo wp_kses_post($after_widget);
  40.      }
  41.      function update($new_instance, $old_instance)
  42.      {
  43.            $instance = $old_instance;
  44.            $instance['title'] = strip_tags($new_instance['title']);
  45.            $instance['number'] = $new_instance['number'];
  46.            $instance['username'] = $new_instance['username'];
  47.            return $instance;
  48.      }
  49.      function form($instance)
  50.      {
  51.            $defaults = array('title' => 'Latest Tweet', 'number' => 3, 'username' => 'username');
  52.            $instance = wp_parse_args((array) $instance, $defaults); ?>
  53.            <p>
  54.                  <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title:','generosity') ?></label>
  55.                  <input class="widefat" style="width: 216px;" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" />
  56.            </p>
  57. <p>
  58.                  <label for="<?php echo esc_attr($this->get_field_id('username')); ?>"><?php _e('Username:','generosity') ?></label>
  59.                  <input class="widefat" style="width: 180px;" id="<?php echo esc_attr($this->get_field_id('username')); ?>" name="<?php echo esc_attr($this->get_field_name('username')); ?>" value="<?php echo esc_attr($instance['username']); ?>" />
  60.            </p>      
  61.            <p>
  62.                  <label for="<?php echo esc_attr($this->get_field_id('number')); ?>"><?php _e('Number of tweets to show:','generosity') ?></label>
  63.                  <input class="widefat" style="width: 30px;" id="<?php echo esc_attr($this->get_field_id('number')); ?>" name="<?php echo esc_attr($this->get_field_name('number')); ?>" value="<?php echo esc_attr($instance['number']); ?>" />
  64.            </p>
  65.      <?php
  66.      }
  67. }
  68. ?>
Correct. There's always a slim chance there's something more than that, but that's the first step you should take, and check for errors afterwards.
Thank you gr8gonzo

The error disappears but now I get a new one :(


Warning: include(/nas/content/live/bdgalardo/wp-content/themes/generosity/widgets/recent-post.php): failed to open stream: No such file or directory in /nas/content/live/bdgalardo/wp-content/themes/generosity/functions.php on line 4

Warning: include(): Failed opening '/nas/content/live/bdgalardo/wp-content/themes/generosity/widgets/recent-post.php' for inclusion (include_path='.:/usr/share/php') in /nas/content/live/bdgalardo/wp-content/themes/generosity/functions.php on line 4

<?php
require_once get_template_directory().'/framework/vafpress/bootstrap.php';

include get_template_directory().('/widgets/recent-post.php');      'this is the line 4 of functions.php

include get_template_directory().('/widgets/popular-post.php');      
include get_template_directory().('/widgets/gallery-slider.php');
include get_template_directory().'/widgets/tags-cloud-number.php';

require_once(get_template_directory().('/include/wp_bootstrap_navwalker.php'));

include(get_template_directory().'/include/helper.php');
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Ryan and gr8gonzo

Yes, I'll contact the hosting company.
Thank you guys, I really appreciate it.