User Submitted Tutorial: About the Classes
Posted: Sat May 17, 2003 9:44 pm
Howdy People.. Well a tutorial hasn't been written for a while, so i thought i'd spend some of the 8 hours i have writing one for you.. The focus of this tutorial will be on establishing Classes and how they can be used, i will be creating a class which i believe will and can be of use to anyone here.
The Stucture of a Class:
That is the structure of most classes, but what are classes?
A class is nothing but a collection of functions which perform a specific task (also called Objects). Each Class can be defined (used) more times on the same page (have multiple instances). And, finally, every class has its own methods (functions) to be then called from within a page.
It's also important to realise that Classes are not just a set of functions, but can be given a set of Global Variables, which is the defining factor between just a file or functions and a class.
Why use classes?
This is a common question and luckily there is a simple answer. You Dont have to, but when you are developing sites that have lots of repetitive code, then Classes can make your life so much simpler. I will demonstrate this in more detail as the article progresses and we start cooking.
Learning in Action
I find the only way to learn something is to work while reading about it. That's why i'm going to run through a class which i'm sure will be useful for those out there that use multiple databases in their site.
The Database Class
OK, now we will start off by creating a file called: Database.class.php
Once that is created we can now start building your class. Start by initiating your php tags, and creating the class called, Database.
Now that, that's done, we can define the class variables.
I'll run you through what these variables will be, although they are pretty self explanitory:
$DatabaseHost = Your Host server (eg. localhost, for many)
$DatabaseUser = Database Username
$DatabasePass = Password for the Username
$DatabaseName = Name of the Database.
It's now time to create the constructor for the Database class.
The constructor has now been built and all the variables have been assigned the value NOT_SET, in other words, nothing. Now, we can start building the goof stuff.
If your wondering what the $this-> is i'll tell you. $this-> is a way to call variables or functions from within a class.
Lets build the DBConnect function:
Now the connection function has been made, we can make the close function.
That was simple. Finally, we can close off the class and start on how this can be used.
Now we have our class called Database. But at the moment its not doing anything, and will continue to do nothing until it is called. So lets call it.
In order to use the function of the class you will need to create a new instance of the class and also include the Database.class.php file into your page. So where you want to create a database connection you would initiate the class like so:
Then you can call the DBConnect() function and define the database variables inside.
This code now gives the variables inside the function values. Now, commonly people dont like putting raw data into pages, and like to define passwords and the like in other files, that can still be done, just include the page with your values and add the variables where i have put the raw values.
OK, now once you've done all your SQL code, you can close the database like so:
Well, i'm not sure whether this is helpful, but i hope it sheds a bit of previously unseen light on the class issue.
The Stucture of a Class:
Code: Select all
<?php
class ClassName
{
// Define Class Variables
var $Foo;
var $Bar;
/* A function which has the same name as the class is known as the constructor, and gives values to the class variables. */
function ClassName()
{
$this->Foo = $Value1;
$this->Bar = $Value2;
}
/* In between the Brackets () are what are known as Parameters, in this instance they are $Foo and $Bar */
function FooBar( $Foo , $Bar )
{
echo "\$Foo is equal to: " . $Foo . " and \$Bar is equal to: " . $Bar . ";
}
}
?>
A class is nothing but a collection of functions which perform a specific task (also called Objects). Each Class can be defined (used) more times on the same page (have multiple instances). And, finally, every class has its own methods (functions) to be then called from within a page.
It's also important to realise that Classes are not just a set of functions, but can be given a set of Global Variables, which is the defining factor between just a file or functions and a class.
Why use classes?
This is a common question and luckily there is a simple answer. You Dont have to, but when you are developing sites that have lots of repetitive code, then Classes can make your life so much simpler. I will demonstrate this in more detail as the article progresses and we start cooking.
Learning in Action
I find the only way to learn something is to work while reading about it. That's why i'm going to run through a class which i'm sure will be useful for those out there that use multiple databases in their site.
The Database Class
OK, now we will start off by creating a file called: Database.class.php
Once that is created we can now start building your class. Start by initiating your php tags, and creating the class called, Database.
Code: Select all
<?php
class Database
{
Code: Select all
var $DatabaseHost;
var $DatabaseUser;
var $DatabasePass;
var $DatabaseName;
$DatabaseHost = Your Host server (eg. localhost, for many)
$DatabaseUser = Database Username
$DatabasePass = Password for the Username
$DatabaseName = Name of the Database.
It's now time to create the constructor for the Database class.
Code: Select all
function Database()
{
$this->DatabaseHost = NOT_SET;
$this->DatabaseUser = NOT_SET;
$this->DatabasePass = NOT_SET;
$this->DatabaseName = NOT_SET;
}
If your wondering what the $this-> is i'll tell you. $this-> is a way to call variables or functions from within a class.
Lets build the DBConnect function:
Code: Select all
function DBConnect( $DatabaseHost , $DatabaseUser, $DatabasePass , $DatabaseName )
{
$ServerConnection = mysql_pconnect( $DatabaseHost , $DatabaseUser , $DatabasePass );
if ( !$ServerConnection )
{
echo( "Sorry, but a connection to the server could not be made " . mysql_error() );
}
$DatabaseConnection = mysql_select_db( $DatabaseName , $ServerConnection );
if ( !$DatabaseConnection )
{
echo( "Sorry, but a connection to the datavase could not be made " . mysql_error() );
}
}
Code: Select all
function DBClose()
{
mysql_close();
}
Code: Select all
}
?>
In order to use the function of the class you will need to create a new instance of the class and also include the Database.class.php file into your page. So where you want to create a database connection you would initiate the class like so:
Code: Select all
require( "Database.class.php" );
$Database = new Database;
Code: Select all
$Database->DBConnect( "localhost" , "username" , "password" , "mydatabase" );
OK, now once you've done all your SQL code, you can close the database like so:
Code: Select all
$Database->DBClose();