Jump to content

My First Class - Feedback / Beginner Questions


Jordban

Recommended Posts

Hello,

 

I would have made the title more descriptive so to help others, but It wouldn't fit.

 

Basically this is my first class. I'm trying hard to figure out how to use classes instead of a file full of functions (my old method).

 

This is a class for a user on a forum i'm developing. Basically on the top of every page a new user object will be created. Then I will be able to pull information from the user as i see fit, and update it. The ParseGold simply adds the commas to the gold amount.

 

<?php

// -----------------------------------------------------------------
// USER CLASS
// GetUserInfo(username,password) returns 1 on failed log in
// UpdateInfo(variable,field,value)
// -----------------------------------------------------------------

class User {

// VARIABLES
var $iUser;
var $iGold;
var $iLang;
var $iCSS;
var $iId;
var $iGroup;
var $iPosts;
var $UserInfo;
var $Auth;
var $Username;
var $Password;
var $CatpureReturn;

// CONSTRUCTOR
function User() {
	$this->iUser = "Guest";
	$this->iGold = 0;
	$this->iLang = 1;
	$this->iCSS = 1;
	$this->iId = NULL;
	$this->iGroup = 0;
	$this->iPosts = 0;
}

// MAKE SURE LOGGED IN -> UPDATE VARIABLES
function GetUserInfo($Username,$Password) {
	$Auth = md5($_SERVER['HTTP_USER_AGENT'].'OMGPLZDONTHACKMEMISTER');
	$Password = md5(md5($Password));
	$UserInfo = query("SELECT * FROM users WHERE username = '$Username' and auth = '$Auth' and password = '$Password'");

	// User was able to log in
	if (numrows($UserInfo)>0){
		$this->iUser = mysql_result($UserInfo,0,'username');
		$this->iGold = mysql_result($UserInfo,0,'gold');
		$this->iLang = mysql_result($UserInfo,0,'language');
		$this->iCSS = mysql_result($UserInfo,0,'css');
		$this->iId = mysql_result($UserInfo,0,'id');
		$this->iGroup = mysql_result($UserInfo,0,'usergroup');
		$this->iPosts = mysql_result($UserInfo,0,'posts');
	} else {
		if ($Userinfo==1) {

			// MySQL Query Failure
			return 2;
		} else {

			// Login Failure
			return 1;
		}
	}

	// END FUNCTION
}

function UpdateInfo($variable,$field,$value) {
	$this->$variable = $value;
	query("UPDATE users SET $field = '$value' WHERE id = '$this->iId'");

	// END FUNCTION
}

function ParseGold() {
	return number_format($this->iGold);

	// END FUNCTION
}

// END CLASS
}


?>

 

And how it might be used..

 

<?php
require("config/connect_to_db.php");
require("includes/classes/User.class.php");

$User = new User();
if ($User->GetUserInfo("username","password") != 1) {
echo "Grats!"
} else {
echo "failed..";
}
$User->UpdateInfo("iGold",'gold',1000);
echo "<br>Now i have more gold.. Gold: ".$User->ParseGold();

?>

 

My Questions:

 

1) What's the point of defining the variables? For memory usage?

2) How important is a deconstructor?

 

Thank you!

 

Jordan

Link to comment
Share on other sites

Are you talking about defining the internal variables? Without them you can't hold anything inside your class.

As for a deconstructor, they have a purpose but you probably won't ever need to define/use one unuless you're planning on doing something just before the object is destroyed (see: active record pattern).

Link to comment
Share on other sites

I'm talking about the use of var to define the properties, and not using __construct() to define the construct.

 

php4 is no longer supported by the php devs, I just think its time to move on. I have been developing in php6 for the last few months, trying to get a headstart.

Link to comment
Share on other sites

Couple other notes...

 

-In your UpdateInfo() method, you update the database, but don't update the value stored in object.

-Also, your GetUserInfo() method has no return value when it succeeds. It really should return TRUE on success and FALSE on failure. It seems like you are trying to use something like return codes for errors. This isn't really the standard for PHP and I would recommend using Exceptions.

Link to comment
Share on other sites

@thorpe: Haha, thank you sir. I do believe my tutorial must have been out of date. I wasn't purposely trying to support php 4, I was just going along with several tutorials, i guess they were outdated.

 

@aschk: You explained it perfectly. Deconstructor is more of a means of doing some last minute business. And yes I was refering to internal variables, thanks for clearifying that.

 

@rhodesa: I do update the local object.

 

$this->$variable = $value;

 

Right?

 

Exceptions.. Gotcha, i'll be updating my code in a minute.

 

So i understand my code is php4, what's the different between php4 and 5?

 

Below i added a php5 consturctor (i believe), as well as using the public, private, protected varibales, and expeption handling (Not really sure on this).

 

<?php

// -----------------------------------------------------------------
// USER CLASS
// GetUserInfo(username,password) returns 1 on failed log in
// UpdateInfo(variable,field,value)
// -----------------------------------------------------------------

class User {

// VARIABLES
public $iUser;
public $iGold;
public $iLang;
public $iCSS;
public $iId;
public $iGroup;
public $iPosts;
public $UserInfo;
public $Auth;
public $Username;
public $Password;

// CONSTRUCTOR
function __construct() {
	$this->iUser = "Guest";
	$this->iGold = 0;
	$this->iLang = 1;
	$this->iCSS = 1;
	$this->iId = NULL;
	$this->iGroup = 0;
	$this->iPosts = 0;
}

// MAKE SURE LOGGED IN -> UPDATE VARIABLES
function GetUserInfo($Username,$Password) {
	$Auth = md5($_SERVER['HTTP_USER_AGENT'].'OMGPLZDONTHACKMEMISTER');
	$Password = md5(md5($Password));
	$UserInfo = query("SELECT * FROM users WHERE username = '$Username' and auth = '$Auth' and password = '$Password'");
	if (!$UserInfo) {
		throw new Exception("Could not query databse.");
	}
	// User was able to log in
	if (numrows($UserInfo)>0){
		$this->iUser = mysql_result($UserInfo,0,'username');
		$this->iGold = mysql_result($UserInfo,0,'gold');
		$this->iLang = mysql_result($UserInfo,0,'language');
		$this->iCSS = mysql_result($UserInfo,0,'css');
		$this->iId = mysql_result($UserInfo,0,'id');
		$this->iGroup = mysql_result($UserInfo,0,'usergroup');
		$this->iPosts = mysql_result($UserInfo,0,'posts');
	} else {
		throw new Exception("Could not log in");
	}

	// END FUNCTION
}

function UpdateInfo($variable,$field,$value) {
	$this->$variable = $value;
	query("UPDATE users SET $field = '$value' WHERE id = '$this->iId'");

	// END FUNCTION
}

function ParseGold() {
	return number_format($this->iGold);

	// END FUNCTION
}

// END CLASS
}


?>

 

and...

 

<?php
require("config/connect_to_db.php");
require("includes/classes/User.class.php");

try {
$User = new User();
if ($User->GetUserInfo("username","password") != 1) {
echo "Grats!";
} else {
echo "failed..";
}
$User->UpdateInfo("iGold",'gold',1000);
echo "<br>Now i have more gold.. Gold: ".$User->ParseGold();
} catch (Exception $e) {
echo $e;

}

?>

 

I'm sure it still needs work. Is there a way to call the exception catch from within the class?

 

Link to comment
Share on other sites

Sorry...didn't see that you updated it...

 

Also, I would just scrap the GetUserInfo() method and make that your constructor. Also, you can specify the default values for your variables when you declare them. This is how I would do it:

 

<?php
class User {

// VARIABLES
public $iUser = "Guest";
public $iGold = 0;
public $iLang = 1;
public $iCSS = 1;
public $iId;
public $iGroup = 0;
public $iPosts = 0;
public $UserInfo;
public $Auth;
public $Username;
public $Password;

// CONSTRUCTOR
function __construct($Username,$Password) {
	$Auth = md5($_SERVER['HTTP_USER_AGENT'].'OMGPLZDONTHACKMEMISTER');
	$Password = md5(md5($Password));
	$UserInfo = query("SELECT * FROM users WHERE username = '$Username' and auth = '$Auth' and password = '$Password'");
	if (!$UserInfo) {
		throw new Exception("Could not query databse.");
	}
	// User was able to log in
	if (numrows($UserInfo)>0){
		$this->iUser = mysql_result($UserInfo,0,'username');
		$this->iGold = mysql_result($UserInfo,0,'gold');
		$this->iLang = mysql_result($UserInfo,0,'language');
		$this->iCSS = mysql_result($UserInfo,0,'css');
		$this->iId = mysql_result($UserInfo,0,'id');
		$this->iGroup = mysql_result($UserInfo,0,'usergroup');
		$this->iPosts = mysql_result($UserInfo,0,'posts');
	} else {
		throw new Exception("Could not log in");
	}
	// END FUNCTION
}

function UpdateInfo($variable,$field,$value) {
	$this->$variable = $value;
	query("UPDATE users SET $field = '$value' WHERE id = '$this->iId'");

	// END FUNCTION
}

function ParseGold() {
	return number_format($this->iGold);

	// END FUNCTION
}

// END CLASS
}
?>

 

and...

 

<?php
require("config/connect_to_db.php");
require("includes/classes/User.class.php");

try {
$User = new User("username","password");
echo "Grats!";
$User->UpdateInfo("iGold",'gold',1000);
echo "<br>Now i have more gold.. Gold: ".$User->ParseGold();
} catch (Exception $e) {
echo "Login failed: ".$e->getMessage();
exit;
}

?>

Link to comment
Share on other sites

@aschk: Thank you for posting sir. I didn't quite understand how the try, catch worked when i asked the question. Now i understand.

 

@rhodesa: Thank you too! I agree that section of the code could go in the constructor, I don't see any instance where that would be a bad thing.

 

Thank you everyone, i'm happy with the code, as long as it appears to be good code and PHP5  ;D

 

Now to get my self into the OOP mindset  ::)

 

Link to comment
Share on other sites

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.