How to include a PHP file which contains $this and gives "PHP Fatal error: Uncaught Error: Using $this when not in object context"

149 Views Asked by At

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?

0

There are 0 best solutions below