Log in

View Full Version : [PHP-Tutorial] Introduction to classes.



Dentafrice,
26-10-2007, 11:38 PM
Well this is a tutorial on the introduction to PHP classes.


Required knowledge:
Basic knowledge of PHP.
Functions.

Lets begin, you know to tell apache to enter PHP mode we need to enter the PHP syntax:


<?php

?>


Lets start to build our class, we are going to make a "check username" class.

First we need to declare that this is going to be a class:



<?php

class check_username
{

}

?>


class - tells PHP this is a class
check_username is the name of the class used later in this tutorial.

Lets begin with a VAR. It is a variable, just like $bob is a variable.


<?php

class check_username
{
var $username;
}

?>

Now, lets begin our "check username" function:


<?php

class check_username
{
var $username;

function check()
{

}
}

?>

That declares that we are ready for a function. Lets add a little to the function to check the username. We need a username to check against so lets add another variable.


<?php

class check_username
{
var $username;
var $c_username;

function check()
{

}
}

?>

Now lets make it check the username:


<?php

class check_username
{
var $username;
var $c_username;

function check()
{
if ($this->username != $this->c_username)
{
echo "That username is not correct!";
}
else
{
echo "That is a correct username!";
}
}
}

?>

if($this->username != $this->c_username)
if the variable $username does not equal the variable $c_username then show "That username is not correct!" otherwise show "That is a correct username!"

$this->username brings the variable $username
$this->c_username brings the variable $c_username.

Now save that as username_class.php

Here is how to use it:



<?php

include "username_class.php"; // Includes class.

$username = new check_username; // Starts the class, check_username is the name we started earlier.
$username->c_username = "Caleb"; // Username to check against.
$username->username = "Bob"; // Username to check.

?>


Comments should help.

Thanks,
Caleb!

Frog!
26-10-2007, 11:45 PM
That's very good, I may start learning too use classes soon :)
EDIT: Fancy getting back to me on that problem Denta?

Dentafrice,
26-10-2007, 11:46 PM
Classes are just... awesome :)

VistaBoy
27-10-2007, 06:56 AM
hmm i maybe wrong but by looking at it, it would show a blank page you would have do to some thing like this


<?php

include "username_class.php"; // Includes class.

$username = new check_username; // Starts the class, check_username is the name we started earlier.
$username->c_username = "Caleb"; // Username to check against.
$username->username = "Bob"; // Username to check.
$username->check() // show the data.

?>
i maybe wrong but yes....

Tomm
27-10-2007, 10:25 AM
To be honest, its really no use learing how to use PHP4-style classes now. As at the end of this year PHP4 will become offically unsupported by PHP. Its best to learn how to use PHP5-style classes. I might write a tutorial on it if people are intrested.

Dentafrice,
27-10-2007, 12:42 PM
$username->check();

Do it tom! :]

Florx
27-10-2007, 06:10 PM
To be honest, its really no use learing how to use PHP4-style classes now. As at the end of this year PHP4 will become offically unsupported by PHP. Its best to learn how to use PHP5-style classes. I might write a tutorial on it if people are intrested.
Yeah go on - im interested :)

Jamie.
29-10-2007, 03:45 PM
Thanks caleb :) Bookmarked.

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