Jump to content

[SOLVED] Conflict with using class functions inside another class


Goldeneye

Recommended Posts

I have three files, class.users.php, class.mysql.php, and register.php

 

Here is a snippet from class.users.php:

<?php 
class user {
	function add($pseudonym, $passkey, $email){
		$pseudonym = mysql_real_escape_string(trim($_POST['pseudonym']));
		$passkey = md5($_POST['passkey']);
		$email = mysql_real_escape_string(trim($_POST['email']));
		if(empty($pseudonym)){return $error = 'The pseudonym field is empty.';}
		if(empty($passkey)){return $error = 'The passkey field is empty.';}
		if(empty($email)){return $error = 'The E-Mail field is empty.';}
		else {
			if(!preg_match('/^[\w_\. -]{3,17}$/', $pseudonym) || preg_match('/[ ]{2,}/', $pseudonym) || (strlen($passkey) < 4) || !preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $email)){
				return $error = 'Either your pseudonym contained invalid characters or consecutive spaces; your passkey was less than 4 characters; or your E-Mail is invalid.';
			} else {
				$query = $sql->query("SELECT `userid` FROM `userbase` WHERE `pseudonym` = '" . $pseudonym . "' OR `email` = '" . $email . "' ") or die(mysql_error());
				if($sql->numRows($query) !== 0){
					return $error = 'The Pseudonym or Email address you submitted are already taken.';
				} else {
					$sql->query("INSERT INTO `userbase` (`pseudonym`, `passkey`, `email`, `echelon`, `joindate`, `tagline`, `stylesheet`, `lastlogin`) VALUES ('" . $pseudonym . "', '" . md5($passkey) . "', '" . $email . "', 1, '" . time() . "', 'about:blank', 'si.primary.css', 0)") or die(mysql_error());
					return $affir = 'You now have yourself an account which means you can <a href="'.p.'login.php">Login</a>';
				}
			}
		}
	}
}
$user = new user;
?>

 

Here is a snippet from class.mysql.php:

<?php
class mysql {
	function query($sql){
		$this->qresult = mysql_query($sql, $this->link);
		if($this->qresult){
			++$this->numQueries;
			return $this->qresult;
		} else {
			$this->savedQueries[] = array($sql, 0);
			return false;
		}
	}

	function result($query = 0, $row = 0){
		return ($query) ? mysql_result($query, $row) : false;
	}

	function fetchAssoc($query = 0){
		return ($query) ? mysql_fetch_assoc($query) : false;
	}

	function fetchRow($query = 0){
		return ($query) ? mysql_fetch_row($query) : false;
	}

	function numRows($query = 0){
		return ($query) ? mysql_num_rows($query) : false;
	}
}
$sql = new mysql;
?>

 

And the snippet from register.php:

<?php
session_start();
define('p', '/foobar/'); //path constant to the scripts
require 'class.mysql.php';
$sql->open('dbhost', 'dbuser', 'dbpass', 'dbname'); //open a database connect using a function class.mysql.php
require 'class.users.php';

//Logged-in users can't register
if(isset($_SESSION['userid'])) header('Location: '.p.'index.php');

//The sumbit button was pressed
if(isset($_POST['action'])){
	//initiate the add function from class.users.php
	$addUser = $user->add($_POST['pseudonym'], $_POST['passkey'], $_POST['email']);

	//the anti-spam lock-and-key did not match, return error. Else, return any error from the add() function in class.users.php
	if($_SESSION['lock'] !== $_POST['key']) $error = 'Anti-Spam-Error: Unmatched lock and key';
	else $error = $addUser;

	//Reset the lock upon an error. Else, return the, "successfully registered," message.
	if($error!=='') $_SESSION['lock']='';
	else $affir = $addUser;
}

//generate a new number for the anti-spam
if($_SESSION['lock']==''){$lock=$data->randNumKey(5); $_SESSION['lock']=$lock;}

echo '<div class="pagetitle">Register</div>';
//display any errors returned by the add() function in class.users.php
echo $error!=='' ? '<p style="color: #FF0000;">'. $error .'</p>' : '';
echo $affir!=='' ? '<p style="color: #00FF00;">'. $affir .'</p>' : '';

//the submit button was not pressed, or all the submitted form data was valid.
if(!isset($_POST['action']) || (isset($_POST['action']) && $affir != $addUser)){
	//show form
}
?>

 

THE PROBLEM:

When the submit button is pressed, I get the Fatal error: Call to a member function on a non-object error when there are no empty form fields. I know why I get this error (it's because I didn't declare the class in class.users.php). So I did try that, and I got the error, You cannot re-declare class mySQL in... (or whatever the exact error is.)

 

THE QUESTION:

How can I set this up so that I can use $sql->query() instead mysql_query() inside the add() function in class.users.php.

 

A preemptive thanks, to anyone who helps.

The reason you are getting errors is because "$sql" doesn't exist inside the user::add function.

 

You can send the $sql to the function via another variable:

 

$addUser = $user->add($_POST['pseudonym'], $_POST['passkey'], $_POST['email'], $sql);

 

OR

 

You can set up the SQL class to have static functions and variables (requires PHP5). This means the SQL functions can be called statically and don't have to be instanced.

 

example:

 

class sql {

private static $numQueries = 0;

static public function query($sql){
  self::$numQueries++;
}

}

class user {
  $query = sql::query("");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.