Jump to content

SIMPLE introduction to OOP


deadonarrival

Recommended Posts

I'm very familiar with PHP. I don't know everything about the syntax and functions yet - but I can do most things and have a vague idea about others.

 

As such I've decided that after about 2 years it's past time I learned how to code in OOP, but I can't find, anywhere, a decent tutorial that starts at a basic, useful level.

 

I come accross tutorials which talk about dog being a class, and lassie being an instance of dog, while bark() is a function lassie, or any other dog can do... and this is all well and good. I can make lassie bark, I can even make lassie only bark if she's eaten.

 

But what the hell lassie has to do with my login system, and how I change these principles into real-world solutions, is boggling me. I follow the first page of the tutorial, then there's a huge jump into all these other things, without any slow progression.

 

I want to be shown how lassie barks, then a few simple things - maybe how to do some sort of counter? Feck, I don't know.... just something basic but that I can actually use, to show me how the principles work, and the interactions between things.

 

If anyone would like to throw me a few tutorials they think can do this, I, Lassie and my sanity will be eternally grateful.

 

<3

Link to comment
Share on other sites

take a quick look at: http://www.stargeek.com/php_login_tutorial.php

 

I changed a couple things around (not tested) but I think you will get the idea.

<?php 

class login(){
function MakeTableLogins($database, $host, $db_user, $db_pass) {//create the logins table
	$linkID = mysql_connect($host, $db_user, $db_pass); 
	mysql_select_db($database, $linkID); 
	mysql_query("create table logins (user char(32), pasword char(32))", $linkID); 
} 

function Encrypt($string) {//hash then encrypt a string 
	$crypted = crypt(md5($string), md5($string));
	return $crypted;
} 

function AddUser($database, $host, $db_user, $db_pass, $username, $password) { //add user to table logins 
	$linkID = mysql_connect($host, $db_user, $db_pass);
	mysql_select_db($database, $linkID);
	$password = $this->Encrypt($password);
	$username = $this->Encrypt($username);
	mysql_query("insert into logins values ('$username', '$password')", $linkID);
} 

function Login($database, $host, $db_user, $db_pass, $user, $password) { //attempt to login false if invalid true if correct 
	$auth = false;
	$user = $this->Encrypt($user);

	$linkID = mysql_connect($host, $db_user, $db_pass);
	mysql_select_db("$database", $linkID);
	$result = mysql_query("select password from logins where user = '$user'", $linkID);
	$pass = mysql_fetch_row($result);
	mysql_close($linkID);

	if ($pass[0] === ($this->Encrypt($password))){
		$auth = true;
	}

	return $auth;
}
}

$l = new login();

$password = 'test12345';
$new_password = $l->Encrypt($password);
echo "This is your new password: ".$new_password;

?>

 

then you can apply it pretty much to anything else. Obviously you should clean up the functions alot, there's no reason to pass DB login information that many times...but I think you'll get it.

 

hope that helps

Link to comment
Share on other sites

Thanks a lot, I think that script's about to do more to help my OOP then every tutorial I've read combined :)

 

Just out of cheek, does anyone have any sort of class for the cleaning of variables? Or would this make more sense as just a function? Something where you pass a variable to it, and it comes back ready to use - for avoiding SQL injection and html tags etc - but without too many 's etc

Link to comment
Share on other sites

i usually have a "functions" class that I have all of that misc stuff in. Here is a really simple example:

<?php

//This file holds the main functions
class func{

//Clean user input for the DB queries
function clean4DB($input){
	$output = mysql_real_escape_string($input);
	return $output;
}

}

$func = new func();
$string = "This is a member's bad input";
$cleaned = $func->clean4DB($string);

//Now you can put $cleaned in a DB

?>

Link to comment
Share on other sites

I usually have a function or two depending on how well I want stuff to be cleaned.... I don't feel like finding the code lol, but I normally go with something like:

 

function QuerySafe($str, $html = false) {
$str = mysql_real_escape_string(addslashes($str));
if($html !== false) {
$str = htmlentities($str);
}
return $str;
}

//if I want to get really strict I might use something like:

function CleanStr($str, $regexp = '/[^a-zA-Z0-9_-]/') {
return preg_replace($str, $regexp);
}

//that I would almost always call to without the second variable.... This doesn't have many uses since you usually want to alert your user if their input is invalid, but I've found uses for it before.

Link to comment
Share on other sites

I think the guide on the main page is great.

 

I am pretty familiar with OOP and even I learned some stuff about Objects specific to PHP! I was quite surprised!

 

Here's the link! http://www.phpfreaks.com/tutorials/150/0.php

 

I'm glad you liked it. Comments like that make it rewarding.

 

There are actually a number of small errors I haven't come around to fixing though. Same goes for part 2, and part 3... Well, part 3 is a really big Word document, littered with comments and self-directions, waiting for me to find the time to work on it.

 

Initially, part 1 was going to touch on Reflection and the SPL as well, but writing the thing took me enough time as it was...  :-\

Link to comment
Share on other sites

OOP sounds incredibly simple until you actually try to apply it and this is the step that you're on now.  You know what objects are, you have some vague ideas about how it may be useful, but then you sit down to write code and you don't know what should be an object and what shouldn't.

 

Unfortunately, this part of OOP is something people learn through experience.  There are no set rules into when you should develop classes, when you should make a base class, etc.  It's something you develop a "feeling" for over time.  My advice is to just try and write anything and do it with objects.

 

Evaluate how you did when you finish.  You may realize halfway through that if you had create Base Class A it would have saved you a lot of typing; you may also realize that the Base Class B you made isn't really all that useful.

 

A great mini-project to help with OOP is to create a dungeon sort of game where you travel through rooms, gather items, and encounter monsters.

Link to comment
Share on other sites

good advise roop. I have some more for you too. What I like to do is to think of what I use all the time. Like clean4DB for example. If I will be using a function a lot, or if it's something fairly complex I will put it into a class. Even something as simple as formatting numbers can help you a lot, and if you design them right they can be pretty helpful.

 

For example I have

function curFormat($price, $dec=2){
	return number_format($price, $dec);
}

in that same functions class that I showed you earlier. So whenever I have a number that I want to format I use

$func->curFormat($number);

 

So if I do

$number = 150;
echo $func->curFormat($number);
//echos 150.00

 

Now say I calculate how long a page takes to load. it would look like this

$page_time = 0.005653133;
echo $func->curFormat($page_time, 6);
//echos 0.005653

 

See how you can easily adapt a simple function to apply to more then one purpose? if so...now you have the idea of OOP!

Link to comment
Share on other sites

Hey, you can always look at Thacmus  ;D [/shameless plug]

It's designed using a mix of procedural and OOP programming, or at least an attemp to do so. For the login system, just look at the source for /db/user.php, /content.php, /admin/content.php, and /admin/page/login.php, if that isn't too much already. You can view these without really downloading the source using the cheap source viewer. The system isn't perfect, and right now the site's running on an older version (it'll always be while I'm still developing it) but you can get a basic idea of how it works. As far as it goes for a tutorial, I don't know, but I can proudly say that I try to comment the living hell out of my code.

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.