Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/83148-static-functions-and-this-or-self/
Share on other sites

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;
	}
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.