Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Problem Issue Error Fatal error: Using $this when not in object context

Fatal error: Using $this when not in object context

We get an error message “Fatal error: Using $this when not in object context” when access CodeIgniter resources within your helper functions or custom library.

For example, we have a helper function called check_login in helpers/site_helper.php that calls session library to determine if a user logged in or not.

helpers/site_helper.php

1
2
3
4
5
6
7
8
9
10
<?php
function check_login()
{
	if($this->session->userdata('id') == "" || $this->session->userdata('email') == "")	{
		redirect(base_url() . 'index.php/auth/login','location');
		exit();
	}
}
?>

<?php function check_login() { if($this->session->userdata('id') == "" || $this->session->userdata('email') == "") { redirect(base_url() . 'index.php/auth/login','location'); exit(); } } ?>

This function will produce a error message:

Fatal error: Using $this when not in object context in D:\_$projects\2. Internal\2.4 4rapiddev.com\_Development\Webfiles\demo\PHP\CodeIgniter\application\helpers\site_helper.php on line 4

Fatal error: Using $this when not in object context in D:\_$projects\2. Internal\2.4 4rapiddev.com\_Development\Webfiles\demo\PHP\CodeIgniter\application\helpers\site_helper.php on line 4

In order to resolve Fatal error: Using $this when not in object context issue, we will need to assign the CodeIgniter object to a variable like this:

$ci = get_instance();

… then use $ci variable instead of $this.

So the helper function above should be like this:

1
2
3
4
5
6
7
8
9
10
11
<?php
function check_login()
{
	$ci = get_instance();	if($ci->session->userdata('id') == "" || $ci->session->userdata('email') == "")
	{
		redirect(base_url() . 'index.php/auth/login','location');
		exit();
	}
}
<?>

<?php function check_login() { $ci = get_instance(); if($ci->session->userdata('id') == "" || $ci->session->userdata('email') == "") { redirect(base_url() . 'index.php/auth/login','location'); exit(); } } <?>

Oct 25, 2014Hoan Huynh
CodeIgniter Send Email Via SMTP Charset UTF-8CodeIgniter Upload And ReSize Image Maintain Ratio
You Might Also Like:
  • Fatal error: Uncaught CurlException: 60: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed thrown
  • Application pool ‘abc’ suffered a fatal communication error with the Windows Process Activation Service
  • CodeIgniter Get Current Controller Function/Action
  • PHP Convert stdClass Object To Array And Array To stdClass Object
  • sphinx install error access is denied
  • CodeIgniter Save Or Log All MySQL Queries
  • Show Error Message On WordPress Custom Login Template Page
  • Validate Date With JQuery And Date Object
  • Configure To Run Codeigniter In Sub Folder Or Sub Directory
  • How To Save PHP Error Log To File In IIS
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

7 years ago Problem Issue Errorbase_url, Codeigniter, CodeIgniter helper function, Fatal error, get_instance1,775
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,451 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,832 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,650 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,985 views
C# Read Json From URL And Parse/Deserialize Json
11,712 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development