PHP Fatal Error: Using $This When Not in Object Context

PHP Fatal error: Using $this when not in object context

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

 foobar::foobarfunc();  // Wrong, it is not static method

but can also be

$foobar = new foobar;  // correct
$foobar->foobarfunc();

You can not invoke the method this way because it is not a static method.

foobar::foobarfunc();

You should instead use:

$foobar->foobarfunc();

If however, you have created a static method something like:

static $foo; // your top variable set as static

public static function foobarfunc() {
return self::$foo;
}

then you can use this:

foobar::foobarfunc();

Using $this when not in object context in PHP

You are not inside instance of class to use $this. Try this, it will work


require_once 'models/Request.php';

$req = new Request;

if(isset($_POST['submit'])){
$data = [
'reqBy' => $_POST['reqBy'],
'off' => $_POST['off'],
'prob' => $_POST['prob']
];

echo "<pre>";
print_r($data);
echo "</pre>";

if($req->addRequest($data)){ //This is the line where it points the error
echo 'Sucess';
}else{
echo 'Something';
}
}
?>

PHP - How to solve error using $this when not in object context?

Your problem is connected with differences between class's methods/properties and object's.

  1. If you define a property as static - you should access it through your class like classname/self/parent ::$property.
  2. If not static - then inside static property like $this->propertie.

For example:

trait Example   
{
protected static $var;
protected $var2;
private static function printSomething()
{
print self::$var;
}
private function doSomething()
{
print $this->var2;
}
}
class NormalClass
{
use Example;
public function otherFunction()
{
self::printSomething();
$this->doSomething();
}
public function setVar($string, $string2)
{
self::$var = $string;
$this->var2 = $string2;
}
}
$obj = new NormalClass();
$obj -> setVar('first', 'second');
$obj -> otherFunction();

Static function printSomething can't access not static propertie $var!
You should define them both not static, or both static.

PHP : Fatal error: Using $this when not in object context in

Here's the problem:

while($oLicence = cLicences::getAll($rLicences)) {

You're trying to access getAll method statically instead of calling it on an instance of the class (object). Therefore you have to object context in this method call, while you're using $this inside.

You should just change it to call method on object that you have already created anyway.:

while($oLicence = $cLicences->getAll($rLicences)) {

Using $this when not in object context php

There are two problems here:

  1. You have defined your methods as static. You should not do that as they are not, they depend on being called on an object as you want to use the objects non-static properties.

  2. You have a typo in your constructor function. The correct name for the constructor is __construct, notice the two _ at the beginning.

Fatal error: Using $this when not in object context in

Like the error says, you can't use $this outside of the class definition. To use $_db outside the class definition, first make it public instead of private:

public $_db

Then, use this code:

$authDb = new AuthDb();
$authDb->_db->prepare($query); // rest of code is the same

--

You have to understand what $this actually means. When used inside a class definition, $this is used to refer to an object of that class. So if you had a function foo inside AuthDB, and you needed to access $_db from within foo, you would use $this to tell PHP that you want the $_db from the same object that foo belongs to.

You might want to read this StackOverflow question: PHP: self vs $this

Fatal Error: Using $this when not in object context

$this will not be available in a static function. You'll probably want to re-create $app within the static function:

static function store($key, $value)
{
$app = base_url('application') . '/cache/';
$key = sha1($key);
$value = serialize($value);
file_put_contents( $app . $key.'.cache', $value);
}

I'm not quite sure what you're trying to do in the grand context of your application, but you may not even need a static method in the first place.



Related Topics



Leave a reply



Submit