Page 1 of 1

Multiple Classes... Im New

Posted: Mon Dec 11, 2006 7:26 pm
by Brokenhope
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I havent worked with PHP in over a year, so I was already off to a bad start, and I just started working with it again, and started by working with classes, I never really used them before.

So right now I have 2 classes, mysqldb, and validateduser. mysqldb establishes the connection to the database, and has all the basic mysql methods I need in it. validateduser allows me to get information about the user, check if theyre logged in, if its a valid login, etc-- by using the database.

Thats where the the problem is.

mysql.php

Code: Select all

<?php
class mysqldb
{
var $dbhost;
var $dbusername;
var $dbpassword;
var $dbname;
var $dblink;

	function connect() {
		$this->dblink = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword);
			if (!$this->dblink) {
			}
			elseif (!mysql_select_db($this->dbname, $this->dblink)) {
				$this->dblink = false;
			}
		return $this->dblink;
	}

	function safe($x) {
		return mysql_real_escape_string($x);
	}

	function query($run) {
		return mysql_query($run, $this->dblink);
	}

	function fetch($query) {
		return mysql_fetch_array($query);
	}

	function num($query) {
		return mysql_num_rows($query);
	}

	function insert($table, $fieldnames, $fieldvalues) {
		return $this->query("INSERT INTO $table ($fieldnames) VALUES ($fieldvalues)");
	}

	function delete($table, $fieldname, $fieldmatch) {
		return $this->query("DELETE FROM $table WHERE $fieldname = '$fieldmatch'");
	}

	function update($table, $set, $where) {
		return $this->query("UPDATE `$table` SET $set WHERE $where");
	}

	function lastinsertid() {
		return mysql_insert_id;
	}

	function close() {
		return mysql_close($this->dblink);
	}
}
?>
That class could have been written much better, but thats what it is for the time being.

validateuser.php

Code: Select all

<?php
class validateduser extends mysqldb
{
	var $vusername;
	var $vpassword;

	function setvariables() {
		$this->vusername = $_SESSION['pkmnt_user'];
		$this->vpassword = $_SESSION['pkmnt_pass'];
	}

	function outputtest() {
		echo 'Validate User Values: '.$this->vusername.$this->vpassword.'<br>';
		echo 'Session Values: '.$_SESSION['pkmnt_user'].$_SESSION['pkmnt_pass'];
	}

	function checkvalid() {
		$sel = "SELECT `id` FROM `members` WHERE username='$this->vusername' AND password='$this->vpassword' LIMIT 1";
		$sel = $this->query($sel);
		$num = $this->num($sel);
		if ($num != 1) {
			return false;
		}
		else {
			return true;
		}
	}
}
?>
-Thats not finished, I only got that far until I realized there was a problem.


Im getting mysql errors because the link-id is invalid, because when it was extended, it didnt extend the variables, including the connection variable, so everything crumbles to pieces... I dont know what the solution is. Ive searched for a solution for the past 8 hours, literaly, plus yesterday, there is not one google result that had any code I could look at to see how I could get this to work, and I was really suprised that php class tutorials brought up very very very few actuall tutorials, and none of them did this.

Heres how theyre tied into the script:

Code: Select all

// Includes
require_once "config.php";
require_once "classes/mysql.php";
require_once "classes/validateuser.php";

$db = new mysqldb;
$db->dbhost = $dbhost;
$db->dbusername = $dbusername;
$db->dbpassword = $dbpassword;
$db->dbname = $dbname;
if(!$db->connect()) {
	echo 'Error Connecting To The Database.';
	exit;
}

// Check if already logged in
$validate = new validateduser;
$validate->setvariables();
Is it possible to extend the variables and connection in the mysql class to the validateduser class? This seems so simple and so absolutely necessary for applications of classes, and yet google has not one result with even a hint at how to do this -_-.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Dec 11, 2006 7:46 pm
by John Cartwright
With your implementation, you'll need to pass a reference of your mysqldb object to the validateduser object. The way you have it setup your using two instances of the mysqldb object, and the one your validateduser object is extending doesn't hold any of the configuration variables you've set. I know you said your kind of new to php, but you may want to look into the "singleton pattern" to carry a single instance of an around. Anyways..

Code: Select all

$validate = new validateduser(&$db); 

class validateduser
{
   $var db;
   //..

   function validateduser(&$db)
   {
     $this->db = $db;
   }

   //..
}

Posted: Mon Dec 11, 2006 8:23 pm
by Brokenhope
Thank you for the reply.

I did come by something on the singleton pattern while I was searching, but had no clue it was for this, they tried to super simplify the example to the point I had no clue what they were doing X_X.

Im not new to PHP just getting back into it, but I am very new to OOP.

Anyways, I thought the singleton pattern only worked with PHP5? Im using PHP4, sorry I forgot to mention that. I tried it anyways and came up with various errors. The first one being:

Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of [runtime function name](). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. in C:\Program Files\Apache Group\httpdocs\pkmtrinitydev\login.php on line 24

Line 24 Being:

Code: Select all

$validate = new validateduser(&$db);
The other errors were var db; not a valid variable... im guessing its supposed to be var $db; and all the other errors were because of the missing $db->, which should all be fixed when the first one is.

Does the singleton pattern work in PHP4? (Im not sure what version I have exactly but its above 4.2, that im sure of)... so do I just need to enable what it says in my PHP.ini file and that will solve the problem?

Posted: Mon Dec 11, 2006 8:27 pm
by sweatje
it is

Code: Select all

$validate =& new validateduser($db);

Posted: Mon Dec 11, 2006 8:39 pm
by Brokenhope
EDIT

Thanks, I got it working. There was another error that took me a few minutes until I realized what was wrong, its no longer $db-> within validateduser, its $this->db->function();

Working class is now

Code: Select all

<?php
class validateduser
{
	var $db;
	var $vusername;
	var $vpassword;

 	function validateduser(&$db)
 	{
 		$this->db = $db;
	}

	function setvariables() {
		$this->vusername = $this->db->safe($_SESSION['pkmnt_user']);
		$this->vpassword = $this->db->safe($_SESSION['pkmnt_pass']);
	}

	function outputtest() {
		echo 'Validate User Values: '.$this->vusername.$this->vpassword.'<br>';
		echo 'Session Values: '.$_SESSION['pkmnt_user'].$_SESSION['pkmnt_pass'];
	}

	function checkvalid() {
		$sel = "SELECT `id` FROM `members` WHERE username='$this->vusername' AND password='$this->vpassword' LIMIT 1";
		$sel = $this->db->query($sel);
		$num = $this->db->num($sel);
		if ($num != 1) {
			return false;
		}
		else {
			return true;
		}
	}
}
?>
Thanks for the help, I would have never figured that out.

Posted: Mon Dec 11, 2006 11:15 pm
by John Cartwright
sweatje wrote:it is

Code: Select all

$validate =& new validateduser($db);
whoops :oops:

Posted: Tue Dec 12, 2006 12:01 am
by Christopher
And if it is PHP4, I think it should be:

Code: Select all

function validateduser(&$db)
        {
               $this->db =& $db;
        }