0

I want to include a file with the path /home/public_html/dir5/file5.php in my php file with path /home/public_html/dir1/file1.php but the problem I get is

PHP Fatal error: Uncaught Error: Using $this when not in object context in /home/public_html/dir5/file5.php:2

The content of file5.php is as follows:

<?php
 if ( !$this->is_users_rating_disabled() ):
    $score = isset( $this->users_data['overall'] ) ? $this->users_data['overall'] : 0;
    $count = isset( $this->users_data['count'] ) ? $this->users_data['count'] : 0;
    $theme = $this->template_field('template_theme', true);
    $maximum_score = $this->template_field('template_maximum_score', true);
     $revOver = $score;
?>

I've read other posts it might be because of the $this like it says in the error log, how can I transform my code to work?

I saw some examples you can transform from:

$this->$db->etc

to:

$db = get_instance();
$db->etc

something like that, but in my case I have $this->users_data['overall'] and $this->template_field('template_maximum_score', true); for example, how can I transform that? Thanks!

Update: is_users_rating_disabled() is defined like below:

public function is_users_rating_disabled()
{

    $auth = $this->preferences_field('preferences_authorization', true);

    if ($auth == 'disabled' || $this->review_field('review_disable_user_rating', true) == 'yes') {
        return true;
    }

    return false;
}

In the end I only want to use $revOver variable from file5.php in file1.php , I tried using GLOBALS['revover'] = $revOver; set in file5.php and using it in file1.php but it's not working, is there any way besides including file5.php in file1.php to use that variable?

  • How is `is_users_rating_disabled()` defined? – user3783243 Nov 26 '21 at 22:24
  • is_users_rating_disabled() is defined like below: public function is_users_rating_disabled() { $auth = $this->preferences_field('preferences_authorization', true); if ($auth == 'disabled' || $this->review_field('review_disable_user_rating', true) == 'yes') { return true; } return false; } – WordPressGeek Nov 26 '21 at 23:00
  • 1
    `public` implies that the function is actually a class _method_. It is a semantic difference, but it is important to note. Classes are a way to isolate and protect code. Inside a class `$this` is used to access protected portions, but only the classes author can use it. Rewriting code to not use `$this` doesn’t really make sense unless the bigger picture can be seen. – Chris Haas Nov 27 '21 at 04:44
  • In the end I only want to use $revOver variable from file5.php in file1.php , I tried using GLOBALS['revover'] = $revOver; set in file5.php and using it in file1.php but it's not working, is there any way besides including file5.php in file1.php to use that variable? – WordPressGeek Nov 27 '21 at 19:35
  • no one knows how to do this? – WordPressGeek Dec 05 '21 at 22:19
  • still searching on this... still digging ... – WordPressGeek Dec 28 '21 at 00:05

0 Answers0