nloding Posted December 25, 2007 Share Posted December 25, 2007 I'm totally lost here. Here's what I'm trying to accomplish: I have a Security object that provides the validation and whatnot for my user authentication script. I thought it would be nice to have it with static functions -- I don't remember why I did it this way -- and now I've run into a debugging error. I can easily fix it by instantiating the class within my User object, and that is how I'm going to fix it for my BETA release this weekend. However, I want to understand why this is happening. The User object calls this function: Security::checkAuth($xPass, $xUser). Here is the relevant code for the Security object: <?php class Security { private $_db; private $_error; // The constructor function __construct() { $this->_db = $factory->getObject('dbSecure'); $this->_error = $factory->getObject('error'); } public static function checkAuth($xUser, $xPass) { // First make sure the username is valid if(self::validateUsername($xUser)) { if(!get_magic_quotes_gpc()) { $xUser = self::slash($xUser); } // Then validate the password if(self::validatePassword($xPass)) { // Encrypt the password and pass it to the db to compare if(!get_magic_quotes_gpc()) { $xPass = self::slash($xPass); } //$xPass = self::encryptPass($xPass); if($this->_db->compareUserPass($xUser, $xPass)) { return true; } else { $this->_error->setError('form', 1); return false; } } else { self::_error->setError('password', 2); return false; } } else { $this->_error->setError('username', 2); return false; } } I have tried 'abstract' in the class definition also. I get this error when I execute the script this way: "Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\WAMP\www\write-new\include\security.inc on line 65" -- line 65 being "self::_error->setError('password', 2); If I change self:: to $this->, I get: "Fatal error: Using $this when not in object context in C:\WAMP\www\write-new\include\security.inc on line 65" Why? And can this object be set up this way? After running into these errors, I realized the Security class is ONLY used for user authentication, so I'm just going to embed the object into the User object; but what is happening above? Quote Link to comment https://forums.phpfreaks.com/topic/83148-static-functions-and-this-or-self/ Share on other sites More sharing options...
corbin Posted December 25, 2007 Share Posted December 25, 2007 $this->_error = $factory->getObject('error'); So I get what you're trying to do there.... But... where is $factory set? Quote Link to comment https://forums.phpfreaks.com/topic/83148-static-functions-and-this-or-self/#findComment-422917 Share on other sites More sharing options...
Daniel0 Posted December 25, 2007 Share Posted December 25, 2007 Well, the Security::checkAuth() method is declared as static and therefore $this is not defined within that method. Just get the error object inside that method. Quote Link to comment https://forums.phpfreaks.com/topic/83148-static-functions-and-this-or-self/#findComment-422999 Share on other sites More sharing options...
emehrkay Posted December 26, 2007 Share Posted December 26, 2007 Static methods should not have $this-> in them. A static method is something that is accessible from outside of the instance of a class. $this-> refers to the instance of the class. self:: allows you call on the other methods/properties in that class without instantiation. You are calling an error class in your constructor that used in your static method. The problem here is that when you statically call that method, the constructor isnt called, so that error class isnt defined. The problem is with your logic setup. If you wanted to keep that method as static, you should move those defining class calls to their own methods like this (this is just an example of how to fix your current code, it may not be the best solution/approach) <?php class Security { private $_db; private $_error; // The constructor function __construct() { } //put them in seperate methods or combine them as one private function getDb(){ self::_db = $factory->getObject('dbSecure'); } private function getError(){ self::_error = $factory->getObject('error'); } public static function checkAuth($xUser, $xPass) { //define the error and db classes self::getDb(); self::getError(); // First make sure the username is valid if(self::validateUsername($xUser)) { if(!get_magic_quotes_gpc()) { $xUser = self::slash($xUser); } // Then validate the password if(self::validatePassword($xPass)) { // Encrypt the password and pass it to the db to compare if(!get_magic_quotes_gpc()) { $xPass = self::slash($xPass); } //$xPass = self::encryptPass($xPass); if(self::_db->compareUserPass($xUser, $xPass)) { return true; } else { self::_error->setError('form', 1); return false; } } else { self::_error->setError('password', 2); return false; } } else { self::_error->setError('username', 2); return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/83148-static-functions-and-this-or-self/#findComment-423552 Share on other sites More sharing options...
nloding Posted December 27, 2007 Author Share Posted December 27, 2007 I get it now. Thank you! For now, I'm just going to instantiate the Security class inside the User class. It's just easier since I'm not using the Security class elsewhere right now. Quote Link to comment https://forums.phpfreaks.com/topic/83148-static-functions-and-this-or-self/#findComment-424225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.