bryanptcs Posted November 13, 2006 Share Posted November 13, 2006 It appears that since I have upgraded to the latest release of PHP that some of my older sites have been having trouble. I have found that $this can no longer be used as it comes up with the following errors:Fatal error: Using $this when not in object context in /home/eliterem/public_html/join.php on line 101The website is elitereminder.comIF you have any suggestions please help. Thank you. Link to comment https://forums.phpfreaks.com/topic/27143-this-fatal-error/ Share on other sites More sharing options...
obsidian Posted November 13, 2006 Share Posted November 13, 2006 How are you using $this? The '$this' variable is reserved in PHP for OOP usage. It is used within a class to reference variables and methods within itself. Could you give us an example of how you are using the variable on the page? If it is not within the confines of a class declaration, this would explain why you're having an issue with it. Link to comment https://forums.phpfreaks.com/topic/27143-this-fatal-error/#findComment-124066 Share on other sites More sharing options...
bryanptcs Posted November 13, 2006 Author Share Posted November 13, 2006 Well, I didn't write the code...the guy before me did....I am not a php guru, but from what I can tell it looks like $this is being used to make sure passwords fall within a specific range....$this->min_pass_length=8; $this->max_pass_length=12; $this->chars='abcdefghijklmnopqrstuvwxyz0123456789';It is used again when someone wants to register with the site...$this->c=@mysql_query("select username from users where username='$susername'"); $this->d=mysql_fetch_object($this->c); if(is_object($this->d)) { $serror="Username is already in use<br>"; } $this->c=@mysql_query("select username from pending where username='$susername'"); $this->d=mysql_fetch_object($this->c);Does any of that help out?? Link to comment https://forums.phpfreaks.com/topic/27143-this-fatal-error/#findComment-124072 Share on other sites More sharing options...
Barand Posted November 13, 2006 Share Posted November 13, 2006 Also, $this cannot be used in static methods which can be used without instantiating an object. [code]<?phpclass foo { private static $myvar = 'Hello World'; static function sayHello1() { echo $this->myvar; } static function sayHello2() { echo self::$myvar; }}foo::sayHello1(); // Fatal error: Using $this when not in object context ... foo::sayHello2(); // OK?>[/code] Link to comment https://forums.phpfreaks.com/topic/27143-this-fatal-error/#findComment-124082 Share on other sites More sharing options...
bryanptcs Posted November 14, 2006 Author Share Posted November 14, 2006 So how could I re-write the following bit of code....I am not a php wizard, I am just cleaning up after someone else. This portion of code is used during registering a user name, it checks the db to see if that username is available and so on....[code]<? } }else if($submit3) { if(!ereg("^[A-Za-z0-9_]{1,16}$",$susername)) { $serror="Invalid username! Use no more than 15 characters and only letters, numbers, and underscores.<br>"; } $this->c=@mysql_query("select username from users where username='$susername'"); $this->d=mysql_fetch_object($this->c); if(is_object($this->d)) { $serror="Username is already in use<br>"; } $this->c=@mysql_query("select username from pending where username='$susername'"); $this->d=mysql_fetch_object($this->c); if(is_object($this->d)) { $serror="Username is already in use<br>"; } if (!$serror) { mt_srand((double)microtime()*1000000^getmypid()); $pass_length = mt_rand($this->min_pass_length,$this->max_pass_length); while(strlen($spassword)<$pass_length) { $spassword.=substr($this->chars,(mt_rand()%strlen($this->chars)),1); } include("include/emails.php"); $signupmessage=str_replace("<username>","$susername",$signupmessage); $signupmessage=str_replace("<password>","$spassword",$signupmessage); $signupmessage=str_replace("<first_name>","$sfirst_name",$signupmessage); $signupmessage=str_replace("<last_name>","$slast_name",$signupmessage); $signupmessage=str_replace("<login_url>","$login_url",$signupmessage); $subject = "$signupsubject"; $message = "$signupmessage"; mail($semail,$subject,$message,"From: $adminemail"); $adminsignupmessage = str_replace("<username>","$susername",$adminsignupmessage); $adminsignupmessage = str_replace("<password>","$spassword",$adminsignupmessage); $adminsignupmessage = str_replace("<first_name>","$sfirst_name",$adminsignupmessage); $adminsignupmessage = str_replace("<last_name>","$slast_name",$adminsignupmessage); $adminsignupmessage = str_replace("<member_email>","$semail",$adminsignupmessage); $subject = "$adminsignupsubject"; $message = "$adminsignupmessage"; mail($adminemail,$subject,$message,"From: $adminemail"); $nowdate = date("M d, Y"); mysql_query("insert into users ( uid, username, password, first_name, last_name, street, city, state, zip, country, email, telephone, last_paid, signup_date ) values ( '','$susername', '$spassword', '$sfirst_name', '$slast_name', '$sstreet', '$scity', '$sstate', '$szip', '$scountry', '$semail', '$stelephone', 'free', '$nowdate' )") or die( mysql_error() ); } if ($serror) {?>[/code] Link to comment https://forums.phpfreaks.com/topic/27143-this-fatal-error/#findComment-124431 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.