Fatal error: Using $this when not in object context
02-06-2006, 11:21 PM
|
Fatal error: Using $this when not in object context
|
Posts: 22
|
I'm getting an error that start with Fatal error: Using $this when not in object context and then goes on to include the file name and a line number.
Googling seems to indicate it's a problem with the script being written for PHP4 and being run on a server with PHP5, but I can't find anything to help me fix it, short of downgrading the server to PHP4, which isn't an option that is available to me.
Does anyone know of a change I can make to the script to accomodate PHP5?
|
|
|
|
02-07-2006, 07:42 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 880
Location: Leeds UK
|
depends what the script is.
Sounds to me though your calling $this from outside a class.
I.E
class test{
var $runtest;
function test(){
$this->runtest = 1; // THIS is HOW ($this) should be used
}
function printThis(){
echo $this->runtest;
}
}
$object = new test();
echo $this->runtest; // fails this not part of an object.
echo $object->runtest; // success
ibbo
|
|
|
|
02-09-2006, 01:53 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 3,113
Location: Toronto, Ontario
|
Either you are using $this outside of a class as ibbo stated, or your class method is using $this and you are calling it statically:
PHP Code:
class TestClass {
var $test;
function testMethod() {
$this->test = 'test';
}
}
TestClass::testMethod(); // Error
Make sure that you make an instance of your class before using it unless you are sure it's supposed to be called statically. When you call a method statically, you don't use any instances and therefore it doesn't have access to any member variables or methods.
|
|
|
|
04-28-2006, 12:39 PM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 2
Location: México.
|
Hello, i'm new to PHP and to this boards, i adapted a sendmail php script with server auth to work with a webform for someone but they changed server and now this script is not working.
As i see, this is because the script is written in php4 and the new server might have php5 maybe someone can help me fix it as i don't have a clue.
Fatal error: Using $this when not in object context in c:\domains\thedomainname.com\wwwroot\class.smtp.in c on line 78
class.smtp.inc line 78: if(!isset($this->status)){
PHP Code:
/*************************************** ** Connect function. This will, when called ** statically, create a new smtp object, ** call the connect function (ie this function) ** and return it. When not called statically, ** it will connect to the server and send ** the HELO command. ***************************************/ function &connect($params = array()){ if(!isset($this->status)){ $obj = new smtp($params); if($obj->connect()){ $obj->status = SMTP_STATUS_CONNECTED; } return $obj; }else{
Thanks.
Last edited by rojo : 04-28-2006 at 12:40 PM.
|
|
|
|
04-28-2006, 07:22 PM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 3,113
Location: Toronto, Ontario
|
Try changing the line to
Code:
if(!isset($this)) {
|
|
|
|
04-29-2006, 12:44 PM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 1
|
wow this is some techinical stuff! LoL
|
|
|
|
05-04-2006, 06:08 PM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 2
Location: México.
|
Thanks a lot Chroder, it Worked.
|
|
|
|
01-07-2007, 10:25 PM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 23
|
I'm also getting a similar error:
Quote:
|
Fatal error: Using $this when not in object context in /path/to/file.php on line 3
|
Here is the file:
Code:
<?php
include_once 'sources/ipsclass.php';
if ( $this->ipsclass->input['act'] != 'Reg' AND $this->ipsclass->input['act'] != 'Login' AND $this->ipsclass->input['act'] != 'post' AND $this->ipsclass->input['act'] != 'Search' AND $this->ipsclass->input['act'] != 'UserCP' AND $this->ipsclass->input['act'] != 'Msg' AND $this->ipsclass->input['showuser'] == '' ) {
echo '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<script type="text/javascript" src="dynifs.js"></script>
</head>
<body>
about 120 lines of html
</body>
</html>
';
}
?>
The included file is an invision board (IPB) file. I'm using php5, which is compatible with IPB's latest version that I'm using.
Thanks for any help given.
btw, if its not too much to ask, is there a better way to make the display of html controlled by an 'if' statement, other than using 'echo'?
|
|
|
|
01-26-2007, 07:31 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 1
|
Hi, I'm having the same problem with a script...these are the lines that are causing the problems:
$this->min_pass_length=8;
$this->max_pass_length=12;
$this->chars='abcdefghijklmnopqrstuvwxyz0123456789';
Anyone who can help me figure this out will have free advertising on my website!

|
|
|
|
01-26-2007, 07:46 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 2,819
Name: Thierry
Location: Latitude 46.79057 :: Longitude 7.119377
|
$this can only be used inside an object.
It's a reference to itself, as you could guess.
From outside the object, you must use the instance name of the object.
PHP Code:
//----- This is the class definition, only executed when doing a "new {class name}" //----- Every reference to an property or method inside the object must be //----- prefixed with $this //----- !!! This is php5 code, it won't work on php4 !!! class clTest{ public $prop1; public $prop2;
public function __construct(){ $this->prop1='doin some tests'; $this->prop2='and checking them'; // This is a method call from the inside of the object $this->test(); }
public function test($txt='this is a method inside clTest'){ echo $txt; } } //---- End of the class definition
//--- PHP executed code starts here $obj= new clTest(); var_dump($obj) /* This should show the content of the object $obj, with prop1 and prop2 set to the text in the object constructor. */
//Now, to modify public properties from outside of the object: $obj->prop2='Changing prop2'; var_dump($obj); /* And here, the value of prop2 has been changed. */
//And as we access the public properties, we can access public methods $obj->test() //Should ouptut "this is a method inside clTest"
Object programmation is a powerful concept, but totally different of sequential programmation.
And there have been a lot of changes between php4 and php5.
PHP4 reference: http://www.php.net/manual/en/language.oop.php
php5 reference: http://www.php.net/manual/en/language.oop5.php
__________________
Trust me, I know what's good for you...
|
|
|
|
10-18-2007, 01:19 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 8
|
---> moved to new location --->
Last edited by thura : 10-22-2007 at 10:29 PM.
|
|
|
|
01-25-2008, 06:06 PM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 1
|
Hi Guys,
Im kinda in the same boat as the rest of the people ... my hosting company decided to update to PHP5 and everything worked on php4, but died in php5 and im at a loss as to what to do ...
I keep getting
Fatal error: Using $this when not in object context in /path.to/calendar.inc.php on line 399
PHP Code:
<? /* ©2004 Proverbs, LLC. All rights reserved. */
if (eregi("calendar.inc.php", $_SERVER['PHP_SELF'])) { // redirect to calendar page header("Location: calendar.php"); exit; }
if(!defined("CALENDAR_PAGE")) { define("CALENDAR_PAGE", TRUE);
require ('base.inc.php');
class calendar_page extends base_calendar { // Constructor function calendar_page() { $this->base_calendar(); }
function createpage($current_day, $current_month, $current_year) { // create the setup object $conf = new layout_setup;
// create the language object $lang = new language;
$adjustment = 3600 * $conf->time_adjustment; if ($conf->use_dst) $adjustment += 3600 * gmdate("I", time() + $adjustment); if ($current_month == "" || $current_month < 1 || $current_month > 12) $current_month = gmdate("n", time() + $adjustment); if ($current_year == "" || $current_year == 0) $current_year = gmdate("Y", time() + $adjustment); if ($current_year > (gmdate("Y", time() + $adjustment) + 10)) $current_year = gmdate("Y", time() + $adjustment) + 10; if ($current_year < (gmdate("Y", time() + $adjustment) - 5)) $current_year = gmdate("Y", time() + $adjustment) - 5; if ($current_year < 1980) $current_year = 1980;
$browser_version = $this->makeheader();
if ($browser_version > 0) { if ($browser_version == 1) $this->pagelayout .= '<link rel="stylesheet" type="text/css" href="navstylesheet.css.php" /> '; if ($browser_version == 2) $this->pagelayout .= '<link rel="stylesheet" type="text/css" href="iestylesheet.css.php" /> ';
$this->pagelayout .= '<script type="text/javascript" src="caljavascript.js.php"></script> <noscript><center><b>'.$lang->word_no_javascript.'</b></center></noscript> '; $this->closeheader(); } else { $this->closeheader(); unset($conf); unset($lang); // require alternate calendar
require ('calendar.alt.php'); $altcalendar = new alt_calendar_page; $this->pagelayout .= $altcalendar->createpage($current_day, $current_month, $current_year); return; }
$this->activeday = $current_day; $this->activemonth = $current_month; $this->activeyear = $current_year;
$nextmonth = $current_month + 1; $nextyear = $current_year; $prevmonth = $current_month - 1; $prevyear = $current_year;
if ($current_month == 1) { $prevmonth = 12; $prevyear = $current_year - 1; } if ($current_month == 12) { $nextmonth = 1; $nextyear = $current_year + 1; }
$this->pagelayout .= '<center> <form method="post" name="calendarPage" action="calendar.php"> <table class="calendar" cellspacing="0" cellpadding="0" border="0"> <tr> <td class="calmenu" style="width: 100%; text-align: center" colspan="7"> <table cellspacing="0" cellpadding="0" border="0"> <tr> <td nowrap class="calmenu" style="width: 250px"> <b>'.$lang->word_today_date.': </b>'.$lang->month_long[gmdate("n", time() + $adjustment)].gmdate(" j, Y", time() + $adjustment).' </td> <td class="calmenu" style="width: 460px; text-align: right"> <b>'.$lang->word_month.': </b> <select name="month"> '; for ($i = 1; $i <= 12; $i++) { $this->pagelayout .= '<option value="'.$i.'"'; if ($i == $this->activemonth) $this->pagelayout .= ' selected'; $this->pagelayout .= '>'.$lang->month_long[$i].'</option> '; }
$this->pagelayout .= '</select> <b>'.$lang->word_year.': </b> <select name="year"> '; for ($i = (gmdate("Y") - 2); $i <= (gmdate("Y") + 10); $i++) { $this->pagelayout .= '<option value="'.$i.'"'; if ($i == $this->activeyear) $this->pagelayout .= ' selected'; $this->pagelayout .= '>'.$i.'</option> '; }
$this->pagelayout .= '</select> <input type="button" style="width: 80px" name="refresh" value="'.$lang->word_refresh.'" onclick="ensureRefresh();" /> <br /> '; if ($conf->show_admin_link) $this->pagelayout .= '<a class="calmenu" href="caladmin/caladmin.php" target="caladmin"><u>'.$lang->word_administration.'</u></a> '; $this->pagelayout .= '</td> </tr> </table> </td> </tr> <tr> <td class="calhead" colspan="7"> <table class="calhead" cellspacing="0" cellpadding="0" border="0"> <tr> <!td class="caltitle" colspan="3"> <input type="hidden" name="day" value="" /> <input type="hidden" name="view" value="calendar" /> '; if (trim($conf->calendar_title_image) != '') $this->pagelayout .= '<img src="'.$conf->calendar_title_image.'" alt="'.$conf->calendar_title_text.'" />'; else $this->pagelayout .= $conf->calendar_title_text;
$this->pagelayout .= ' </td> </tr> <tr> <td class="calmonths" style="text-align: left"> '; if ($prevyear > 1979 && $prevyear > (gmdate("Y", time()) - 6)) $this->pagelayout .= '<a class="calmonths" href="calendar.php?month='.$prevmonth.'&year='.$prevyear.'"><b><< '.$lang->month_long[$prevmonth].' '.$prevyear.'</b></a>';
$this->pagelayout .= ' </td> <td class="calmonths" style="width: 300px; font-size: 16px"> <b>'.$lang->month_long[$current_month].' '.$current_year.'</b> </td> <td class="calmonths" style="text-align: right"> '; if ($nextyear < (gmdate("Y", time()) + 11)) $this->pagelayout .= '<a class="calmonths" href="calendar.php?month='.$nextmonth.'&year='.$nextyear.'"><b>'.$lang->month_long[$nextmonth].' '.$nextyear.' >></b></a>'; else $this->pagelayout .= ' ';
$this->pagelayout .= ' </td> </tr> </table> </td> </tr> <tr> '; if (isset($conf->calendar_start_day) && $conf->calendar_start_day <= 6 && $conf->calendar_start_day >= 0) $n = $conf->calendar_start_day; else $n = 0;
for ($i = 0; $i < 7; $i++) { if ($n > 6) $n = 0;
$this->pagelayout .= '<td class="calweekday" nowrap> <b>'.$lang->day_long[$n].'</b> </td> '; $n++; } $this->pagelayout .= '</tr> ';
$this->makecalendar($current_month, $current_year, $conf->calendar_start_day);
$this->pagelayout .= '</table> </form> </TD> </TR> </TABLE> </TD> </TR>
<TR> <TD> <TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0 align=middle> <TR> <TD WIDTH=100% HEIGHT=22 bgCOLOR="#003A99" align="center"> <a href="index.php" class="tahbol">Home</a> <a href="nstcompany.php" class="tahbol">About Us</a> <a href="courses.php" class="tahbol">Course Dates</a> <a href="new.php" class="tahbol">Whats New</a> <a href="links.php" class="tahbol">Links</a> <a href="contact.php" class="tahbol">Contact US</a> </TD> </TR> <TR> <TD WIDTH=770 HEIGHT=18 align="center"> <font class="ar10">
<div style="padding:0" class="tah11"> <table width=90% border=0 cellpadding=5 > <tr> <td width=100% class=t05> <center> <b>National Safety & Training Co Ltd,</b> Greenane, Kanturk, Co.Cork.<br> <font class=t02> <b>E-mail:</b> <a href="mailto:info@nstireland.com"><font class=t03>info@nstireland.com</a> <font class=t02> <b>LoCall:</b> <font class=t03> 1890 512 512</br>
</td> </tr> </table> </center>
</div>
</TD> </TR> </TABLE> </TD> </TR> </TABLE> </TD> </TR> </TABLE> </BODY> </HTML>'; unset($conf); unset($lang); }
function makecalendar($current_month, $current_year, $startweekday) { $firstweekday = gmdate("w", gmmktime(0, 0, 0, $current_month, 1, $current_year));
$lastday = 28;
for ($i = $lastday; $i < 32; $i++) { if (checkdate($current_month, $i, $current_year)) $lastday = $i; }
$todayday = 0;
$conf = new layout_setup; $adjustment = 3600 * $conf->time_adjustment; if ($conf->use_dst) $adjustment += 3600 * date("I", time() + $adjustment);
if ($current_month == gmdate("n", time() + $adjustment) && $current_year == gmdate("Y", time() + $adjustment)) $todayday = gmdate("j", time() + $adjustment);
$calday = 1; $calaccess = new caldbaccess; while ($calday <= $lastday) { $this->pagelayout .= '<tr> '; for ($j = 0; $j < 7; $j++) { if ($j == 0) $n = $startweekday; else { if ($n < 6) $n = $n + 1; else $n = 0; } if (($calday == 1 && $firstweekday == $n) || ($calday > 1 && $calday <= $lastday)) { $linesoftext = Array(); $itemcount = $calaccess->GetCalendarDateEvents($calday, $current_month, $current_year); for ($x = 0; $x < $itemcount; $x++) { $calaccess->next_record(); $linesoftext[] = $calaccess->f('short_description'); }
$itemcount = $calaccess->GetCalendarDayEvents($calday, $current_month, $current_year); for ($x = 0; $x < $itemcount; $x++) { $calaccess->next_record(); $linesoftext[] = $calaccess->f('short_description'); }
if ($todayday > 0 && $todayday == $calday) $this->addcalday($calday, true, $linesoftext); else $this->addcalday($calday, false, $linesoftext); $calday++; } else $this->pagelayout .= '<td class="calday" style="cursor: default"> </td> '; } unset ($caldb); $this->pagelayout .= '</tr> '; } unset($conf); }
function addcalday($curday, $istoday, $textdisplay) { // create the setup object $tempconf = new layout_setup;
if ($istoday) $this->pagelayout .= '<td nowrap class="calday" style="background-color: #'.$tempconf->calendar_today_background_color.'" onmouseover="tdmv(this)" onmouseout="tdtmo(this)" onclick="sendDay('.$curday.', '.$this->activemonth.', '.$this->activeyear.')"> '.$curday.'<br /> '; else $this->pagelayout .= '<td nowrap class="calday" onmouseover="tdmv(this)" onmouseout="tdmu(this)" onclick="sendDay('.$curday.', '.$this->activemonth.', '.$this->activeyear.')"> '.$curday.'<br /> ';
unset($tempconf);
// create the language object $templang = new language;
if ($textdisplay != "" && count($textdisplay) > 0) { for ($i = 0; $i < count($textdisplay); $i++) { $displine = ''; if (strlen(trim($textdisplay[$i])) > 21) $displine = trim(substr(trim($textdisplay[$i]), 0, 18)).'...'; else $displine = trim($textdisplay[$i]); $displine .= '<br /> '; if ($i < 4) $this->pagelayout .= $displine; if ($i == 5) $this->pagelayout .= '<center><< '.$templang->word_more.' >></center> '; } }
unset($templang);
$this->pagelayout .= '</td> '; } } } // Create the calendar object. $page = new calendar_page;
$this->pagelayout .= " include('../footer.php'); ";
?>
|
|
|
|
02-22-2008, 03:04 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 2
|
i`m having same problem here
Fatal error: Using $this when not in object context in /path/lookup.php on line 29
Code:
session_start();
require("config.php");
include_once("session.php");
include("templates/main.html.php");
include("templates/leftside.html.php");
eval("\$header = \"".gettemplate("templates/search_header")."\";");
echo $header;
include("sponsors.php");
//################# send information ########################
if (isset($_POST["act"]) AND $_POST["act"] == "pwchg"){
$UserInfo =& new User($_SESSION['id']);
$UserInfo->LookupUserPswd();
if ($this->_userValues['UserError']){
$error= $this->_userValues['UserError'];
include("templates/emailform.html.php");
} else {
$Message = "<div class='error'>$PasswordChangeSuccess<br /></div>";
eval("\$up = \"".gettemplate("templates/table_row")."\";");
echo $up;
}
include("templates/footer.html.php");
exit;
}
include("templates/emailform.html.php");
include("templates/footer.html.php");
?>
|
|
|
|
02-22-2008, 04:16 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 2,819
Name: Thierry
Location: Latitude 46.79057 :: Longitude 7.119377
|
PHP Code:
$UserInfo =& new User($_SESSION['id']); $UserInfo->LookupUserPswd(); if ($this->_userValues['UserError']){ $error= $this->_userValues['UserError']; include("templates/emailform.html.php");
Hmmm, just a guess, but the script instantiate a "User" object.
Maybe the $this should report to the user object:
PHP Code:
$UserInfo =& new User($_SESSION['id']); $UserInfo->LookupUserPswd(); if ($UserInfo->_userValues['UserError']){ $error= $UserInfo->_userValues['UserError']; include("templates/emailform.html.php");
__________________
Trust me, I know what's good for you...
|
|
|
|
02-22-2008, 04:18 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 2,819
Name: Thierry
Location: Latitude 46.79057 :: Longitude 7.119377
|
Quote:
PHP Code:
$page = new calendar_page; $this->pagelayout .= " include('../footer.php'); ";
|
And here too, I'm taking a guess, but try
PHP Code:
$page = new calendar_page; $page->pagelayout .= " include('../footer.php'); ";
__________________
Trust me, I know what's good for you...
|
|
|
|
02-22-2008, 04:55 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 2
|
thanks tripy it worked 
|
|
|
|
04-30-2008, 10:54 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 3
Name: Joe
|
Same issue here gents. Is this the PHP4/5 issue aswell
Code:
function fetch () {
if ( $row=pg_fetch_array($this->query,PGSQL_ASSOC) ) {
return $row;
} else if ( $this->size() > 0 ) {
pg_data_seek($this->query,0);
return false;
} else {
return false;
}
|
|
|
|
04-30-2008, 11:02 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 957
|
Try using a closing brace on your function: }
|
|
|
|
04-30-2008, 11:39 AM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 3
Name: Joe
|
cheers dude, where exactly? also Im designing this in postgresql and im looking for the equivalent of
.
Cheers for your help. Its been a long day of staring at this thing
Regards
Jez
Update: Sorry found the brace. If there was an emoticon for an idiot I would use it next to my name. But does anyone know of a line of code for the one above
Last edited by Jezarel : 04-30-2008 at 11:45 AM.
|
|
|
|
04-30-2008, 12:07 PM
|
Re: Fatal error: Using $this when not in object context
|
Posts: 957
|
Is this what you're looking for?
|
|
|
|
|
« Reply to Fatal error: Using $this when not in object context
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|