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';
}
}
?>
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.
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)) {
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.
- If you define a property as static - you should access it through your class like classname/
self
/parent
::$property
. - 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.
Fatal error: Using $this when not in object context in E:\xampp\htdocs\
$role = userrole::get_premium_subscritpion(1);
I notice that you are trying to call a non-static function in a static way
You may either change the function to static or change the way you call this function
public static function get_premium_subscritpion {
or
$obj = new userrole();
$role = $obj->get_premium_subscritpion(1);
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
Related Topics
When to Pass-By-Reference in PHP
How to Set the Env Variable for PHP
Hide Shipping Methods for Specific Shipping Class in Woocommerce
Array_Multisort and Dynamic Variable Options
How to Get Customer Details from an Order in Woocommerce
Bounce Email Handling with PHP
Recursive Categories with a Single Query
How to Make a String in PHP with a Backslash in It
What's the Disadvantage of Mt_Rand
PHP Form Send Email to Multiple Recipients
How to Check If a Video Exists on Youtube, Using PHP
How to Add a Delete Button to a PHP Form That Will Delete a Row from a MySQL Table
How to Group by and Sum PHP Array
PHP Technique to Query the Apns Feedback Server
Dyld: Library Not Loaded: /Usr/Local/Lib/Libpng16.16.Dylib with Anything PHP Related
How to Change/Custom Password Field Name for Laravel 4 and Laravel 5 User Authentication