Page 1 of 1
Classes
Posted: Sat Oct 31, 2009 1:47 pm
by jont
So I'm not exactly new to PHP but I am new to OOP and classes.
I'm currently attempting to build a little cms, all is going well and im attempting to use classes in it, but I think I may be using them incorrectly.
For example i've got the file member.class.php which holds the class member. Inside the class are numerous functions, one for login, one for logout etc.
Can anyone point me in the direction of some decent tutorials, the only ones I seem to find are fairly confusing?
I've stuck my member.class.php up here
http://pastebin.com/m43f42fd2 maybe someone could take a look and tell me where I'm going wrong?
Re: Classes
Posted: Sat Oct 31, 2009 3:36 pm
by markusn00b
I'll be blunt - that's not OOP. You've simply encapsulated all these different functions into a class. It may be a class, but it definitely ain't OOP.
However, don't feel like I'm trying to put you down - far from it. We've all been there... incredibly intimidated by OOP, but after reading about it, practising with it, etc., you get the feel for it.
The idea of an object (effectively, your class) is that itself and its members should all be related and work together to achieve some single task. What you have at the moment is a class with completely unrelated methods achieving many different tasks.
If you're serious about understanding OOP, go to your library and find some books on it - the language is irrelevant (usually) as the principles are common, only the semantics change.
Re: Classes
Posted: Sat Oct 31, 2009 3:39 pm
by jont
ahh that's pretty much what I thought I was doing.
Am I right in thinking that each function I have in that class like login, logout etc should be a seperate class made up of functions?
Could someone show me a basic example of what something like a login class would like? Maybe convert my function if possible?
Code: Select all
/*
* @function login
* display the login form or process it if it's been submitted
*/
function login()
{
$db = new db;
$db->connect(); // connect to database
// if the form has been submitted start processing it
if(isset($_POST['submit']))
{
$username = $_POST["username"]; // get the username
$password = $_POST["password"]; // get the password
$error = ''; // no errors
$validinput = new validinput; // start the valid input class
// check the username and password are valid, if not add a warning message to $error
$error .= $validinput->check('username', $username);
$error .= $validinput->check('password', $password);
// if theres any errors stop processing the form and display the errors and login form
if(!empty($error))
{
echo '<strong>There were some errors...</strong><br />';
echo $error . '<br />';
include("inc/forms/login.form.php");
}
// If there is no $error continue the process
else
{
$password = md5($password); // md5 the passord
$user_query = $db->query("SELECT * FROM member WHERE username = '$username' AND password = '$password' LIMIT 1"); // SQL Select to get the member
if($db->num_rows($user_query) == 1) // make sure there was a match, and only one match
{
$row = $db->fetch_assoc($user_query);
if($row['level_id'] == "4") // if the account is not verified stop processing and display an error message
{
echo '<strong>You need to verify your account</strong>';
echo '<meta http-equiv=Refresh content=2;url=index.php>';
exit();
}
// set up the session stuff
$_SESSION['logged'] = true;
$_SESSION['member_id'] = $row['member_id'];
$_SESSION['username'] = $row['username'];
$_SESSION['level_id'] = $row['level_id'];
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
// set a few vars ready to log the user in the database
$sessionid = session_id();
$ip = $_SERVER['REMOTE_ADDR'];
$member_id = $row['member_id'];
$username = $row['username'];
$date = time();
// update the database and log the user in
$log_user_in = $db->query("UPDATE member SET lastlogin = '$date', ip = '$ip', sessionid = '$sessionid' WHERE member_id = '$member_id' AND username = '$username' LIMIT 1 ;");
// if all went well display a success message and redirect to the home page
if($log_user_in)
{
echo '<strong>you are now logged in</strong>';
echo '<meta http-equiv=Refresh content=2;url=index.php>';
}
// if something went wrong with the login display an error and the login form
else
{
echo '<strong>There was an error, please try again</strong><br /><br />';
include("inc/forms/login.form.php");
}
}
// if the username or password are incorrect display an error message and the login form
else
{
echo '<strong>Username or Password incorrect</strong><br /><br />';
include("inc/forms/login.form.php");
}
}
}
else // if the form has not been submitted display the login form
{
include("inc/forms/login.form.php");
}
}