Page 1 of 1

inheritance problem or what?

Posted: Sat Apr 23, 2005 4:55 am
by mudkicker
Hey people. I am writing a class and there is a problem there.

Code: Select all

<?php
class mudAuth
{
	protected $username;
	protected $password;
	protected $additionalinfo;
	protected $authtype;

	function set_info($username, $password, $additional = array())
	{
		$this->username = $username;
		$this->password = $password;
		$this->additionalinfo = $additional;
	}

	function set_auth_type($type)
	{
		$class = $type . 'Auth';
		if (class_exists($class)) {
			$this->authtype = new $class;
		}
		else {
			trigger_error('There is not such a type of authorization supplied by this class.<br />Available are: DB, TXT, HTTP, XML', E_USER_WARNING);
			exit;
		}
	}

	function check_info()
	{
		return $this->authtype->control();
	}
}

class DBAuth extends mudAuth
{	
	protected function control()
	{
		$controlarray = array("hostname", "username", "password", "databasename", "tablename", "usernamecolumn", "passwordcolumn");
				
		foreach ($controlarray as $celement)
		{
			if (!array_key_exists($celement, $this->additionalinfo)) {
				trigger_error('A required information for this authorization type is not given: <span style="color:red;">'. $celement .'</span>', E_USER_WARNING);
				exit;
			}
		}

		@mysql_connect($this->additionalinfo['hostname'], $this->additionalinfo['username'], $this->additionalinfo['password']) or trigger_error('Connection to MySQL aborted. Reason: '. mysql_error(), E_USER_WARNING);
		@mysql_select_db($this->additionalinfo['databasename']) or trigger_error('Database selection aborted. Reason: '. mysql_error(), E_USER_WARNING);

		$query = @mysql_query("SELECT * FROM ". $this->additionalinfo['tablename'] ." WHERE ". $this->additionalinfo['usernamecolumn'] ." = '". $this->username ."' AND ". $this->additionalinfo['passwordcolumn'] ." = '". $this->password ."'");

		if (mysql_num_rows($query) > 0) {
			return true;
		}
		else {
			return false;
		}
	}
}
?>
if i run this code, it always says that

Code: Select all

Warning: array_key_exists() &#1111;function.array-key-exists]: The second argument should be either an array or an object in c:\wamp\www\test\mudAuth.php on line 43

Warning: A required information for this authorization type is not given: hostname in c:\wamp\www\test\mudAuth.php on line 44
So, it is that $this->additionalinfo is ignored in DBAuth child class... But why.. I'm so confused, please help! :( :?

Posted: Sun Apr 24, 2005 12:29 am
by mudkicker
Any ideas?

Re: inheritance

Posted: Mon Apr 25, 2005 4:24 am
by Black Unicorn
Looks like

Code: Select all

$controlarray = array("hostname", "username", "password", "databasename", "tablename", "usernamecolumn", "passwordcolumn");
could be better represented as

Code: Select all

$controlarray = array(
 "hostname"=>'',
 "username"=>'',
 "password"=>'',
 "databasename"=>'',
 "tablename"=>'',
 "usernamecolumn"=>'',
 "passwordcolumn"=>'')
It's just a guess, but try it.

H