PHP Fatal error Using this when not in object context

0 votes

I am working on a new WebApp without a Framework. 

In my index.php I'm using: require_once('load.php');

And in load.php I'm using require_once('class.php'); to load my class.php.

I am getting this error in my class.php:

Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)

class.php :

class foobar {

    public $foo;

    public function __construct() {
        global $foo;

        $this->foo = $foo;
    }

    public function foobarfunc() {
        return $this->foo();
    }

    public function foo() {
        return $this->foo;
    }
}

In my index.php I'm loading maybe foobarfunc() like this:

foobar::foobarfunc();

Can someone please help me with this error?

Jun 18, 2022 in PHP by narikkadan
• 63,600 points
1,025 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer
0 votes
1 answer

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in .?

Hello @kartik, You may get this error because ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,840 points
2,280 views
0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,840 points
2,056 views
0 votes
1 answer

Fatal error: Cannot access empty property in C:\xampp\htdocs\MyTestPages\f.php on line 15

Hello @kartik, You access the property in the ...READ MORE

answered Oct 19, 2020 in PHP by Niroj
• 82,840 points
1,875 views
0 votes
1 answer

Fatal error: Call to undefined function imap_open() in PHP

Hello @kartik, In Ubuntu for install imap use sudo ...READ MORE

answered Oct 29, 2020 in PHP by Niroj
• 82,840 points
5,638 views
0 votes
1 answer

Php exec() not returning error message in output

Hello @kartik, You need to capture the stderr too. Redirecting stderr to stdout should do ...READ MORE

answered Nov 18, 2020 in PHP by Niroj
• 82,840 points
5,097 views
0 votes
0 answers

Can I use PHP reserved names for my functions and classes?

I wanted to create a function called ...READ MORE

Jun 12, 2022 in PHP by narikkadan
• 63,600 points
419 views
0 votes
1 answer

Python class inherits object

Python 3.x: class MyClass(object): = new-style class class MyClass: = new-style ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,020 points
804 views
0 votes
4 answers

How to print instances of a class using print()?

You need to implement your own __repr__ ...READ MORE

answered Nov 25, 2020 in Python by Saksham Azad
1,221 views
0 votes
1 answer

How are Python metaclasses different from regular class inheritance?

Python 3 class MyClass(object): = New-style class class MyClass: = New-style ...READ MORE

answered Nov 20, 2020 in Python by Gitika
• 65,770 points
532 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP