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();