PDA

View Full Version : [TUT] PHP Classes & Functions



Blob
26-05-2007, 09:29 PM
Ok, well I was bored and decided to write this.

Classes are mainly used when you have lots of functions doing stuff for the same thing, e.g. you might have functions which displays the users level, username etc, so instead of indivitual ones you can group them together in a class.

Lets learn about functions:

Functions are used to make up your own functions hidden away in a file so no one looks at your rubbish coding and only see a good function (well for me it is..)

Anyway, in your function you can use allready made functions like this:



<?php
function my($mum, $likes, $you)
{
if($mum == "is" && $likes = "your" && $you == "lover")
{
echo $mum;
echo $likes;
echo "<br />";
echo $you;
}
}

my("is", "your", "lover");
?>
Lets split it down!


function my($mum, $likes, $you)
{That defines the functions name, and what variables to use in it


if($mum == "is" && $likes = "your" && $you == "lover")
{If your using functions you should know what it does, but it uses the variables defined in the function


echo $mum;
echo $likes;
echo "<br />";
echo $you;Echos the variables in the functions


}
}

my("is", "your", "lover");Ends the function and the if, and uses the functions.

Ok, now onto classes:

If you had multiple functions (these will just be the same)

You would do this:



<?php
class mum
{
function my($mum, $likes, $you)
{
if($mum == "is" && $likes = "your" && $you == "lover")
{
echo $mum;
echo $likes;
echo "<br />";
echo $you;
}
}

function ha($mum, $likes, $you)
{
if($mum == "is" && $likes = "your" && $you == "lover")
{
echo $mum;
echo $likes;
echo "<br />";
echo $you;
}
}
}
$any variable name here = new classnamehere(mum in our case)();
$the same variable as above->functionname("is", "your", "lover");
$the same variable as above->functionname("is", "your", "lover");
// So it would be:
$example = new classnamehere(mum in our case)();
$example->my("is", "your", "lover");
$example->ha("is", "your", "lover");
?>
I dont really need to explain it, as if you know PHP and are using classes you would understand it.

Edited by Nick- (Forum Super Moderator): Thread moved from Website Designing.

Mentor
26-05-2007, 11:59 PM
Ok tutorial, although really random and useless example function lol.

Blob
27-05-2007, 08:38 AM
Ok tutorial, although really random and useless example function lol.

I wanted to see if people could make sence out of the = "wordshere" and the $wordshere :P

Luckyrare
27-05-2007, 08:53 AM
Even though I understand how to use both it was hard to follow.

Blob
27-05-2007, 08:54 AM
Even though I understand how to use both it was hard to follow.

What you mean..

Luckyrare
27-05-2007, 09:07 AM
What you mean..

Coding tutorials need loads of comments in them, explaining what everything does. Plus a more realistic use would be better for the final example.

Want to hide these adverts? Register an account for free!