Creating a Credit Card Validation Class With PHP - Creating the CCreditCard class
(Page 3 of 6 )
Let's now create a PHP class that we can use to store and validate the details of a credit card. Our class will be able to hold the cardholders name, the card type (mastercard, visa, etc), the card number, and the expiry month and date.
Create a new PHP file called class.creditcard.php. As we walk through the following two pages, copy-paste each piece of code shown to the file and save it.
We start of by defining several card type constants. These values will be used to represent the type of card that our class will be validating:
<?php
define("CARD_TYPE_MC", 0);
define("CARD_TYPE_VS", 1);
define("CARD_TYPE_AX", 2);
define("CARD_TYPE_DC", 3);
define("CARD_TYPE_DS", 4);
define("CARD_TYPE_JC", 5);Next, we have our class declaration. Our class is called CCreditCard. Note that there is an extra 'C' at the front of the class name intentionally: it's a common programming practice to prefix the name of a class with 'C' to in fact indicate that it is a class.
We also define five member variables, which will be used internally to hold the credit cards name, type, number, expiry month and year respectively:
class CCreditCard
{
// Class Members
var $__ccName = '';
var $__ccType = '';
var $__ccNum = '';
var $__ccExpM = 0;
var $__ccExpY = 0;Next we have our class's custom constructor. A constructor is a function that has the same names as the class in which it exists. It accepts no arguments and returns no values. It is special in a sense that it is automatically executed whenever we create a new instance of that class.
Whenever we want to create a new instance of our CCreditCard class, we must explicitly pass in five arguments to its constructor: the cardholders name, card type, number, and expiry date. Because we have created our own custom constructor (PHP implements a default constructor that accepts no arguments if we don’t explicitly create one), we must pass in values for each of these five arguments every time we instantiate the class. If we omit them then PHP will raise an error.
// Constructor
function CCreditCard($name, $type, $num, $expm, $expy)
{If the value of the $name variable passed into the constructor is empty, then we use the die() function to terminate the instantiation of our class and output an error message telling the user that they must pass a name to the constructor:
// Set member variables
if(!empty($name))
{
$this->__ccName = $name;
}
else
{
die('Must pass name to constructor');
}To make our CCreditCard class flexible, it accepts several different ways to specify the type of card that is being stored. For example, if we want to add the details of a mastercard to a new instance of our CCreditCard class, then we could pass in the following values for the $type variable of the constructor: "mc", "mastercard", "m", or "1".
We make sure that a valid card type has been passed in, and set the value of our classes $__ccType variable to one of the constant card type values that we defined earlier:
// Make sure card type is valid
switch(strtolower($type))
{
case 'mc':
case 'mastercard':
case 'm':
case '1':
{
$this->__ccType = CARD_TYPE_MC;
break;
}
case 'vs':
case 'visa':
case 'v':
case '2':
{
$this->__ccType = CARD_TYPE_VS;
break;
}
case 'ax':
case 'american express':
case 'a':
case '3':
{
$this->__ccType = CARD_TYPE_AX;
break;
}
case 'dc':
case 'diners club':
case '4':
{
$this->__ccType = CARD_TYPE_DC;
break;
}
case 'ds':
case 'discover':
case '5':
{
$this->__ccType = CARD_TYPE_DS;
break;
}
case 'jc':
case 'jcb':
case '6':
{
$this->__ccType = CARD_TYPE_JC;
break;
}
default:
{
die('Invalid type ' . $type . ' passed to constructor');
}
}If an invalid card type is passed in, then the default branch of our switch statement will be called, resulting in our script terminating with the die() function.
We can take advantage of PHP's built-in support for regular expressions by using the ereg_replace function to strip out all non-numeric characters from the credit card number:
// Don't check the number yet,
// just kill all non numerics
if(!empty($num))
{
$cardNumber = ereg_replace("[^0-9]", "", $num);
// Make sure the card number isnt empty
if(!empty($cardNumber))
{
$this->__ccNum = $cardNumber;
}
else
{
die('Must pass number to constructor');
}
}
else
{
die('Must pass number to constructor');
}We finish off our CCreditCard constructor by making sure that both the expiry month and year are valid, numerical values:
if(!is_numeric($expm) || $expm < 0 || $expm > 12)
{
die('Invalid expiry month of ' . $expm . ' passed to constructor');
}
else
{
$this->__ccExpM = $expm;
}
// Get the current year
$currentYear = date('Y');
settype($currentYear, 'integer');
if(!is_numeric($expy) || $expy < $currentYear || $expy > $currentYear + 10)
{
die('Invalid expiry year of ' . $expy . ' passed to constructor');
}
else
{
$this->__ccExpY = $expy;
}
}Next: Creating the CCreditCard class (contd.) >>
More PHP Articles
More By David Rusik