PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Stephen P   Class Generator for MySQL Tables   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example Script, shows you how to use this class.
Class: Class Generator for MySQL Tables
Generate classes to access MySQL database tables
Author: By
Last change:
Date: 16 years ago
Size: 1,852 bytes
 

Contents

Class file image Download
<?php
/**
  Example file for classGenerator.class.php (ClassGenerator Class)
  Author: Stephen Powis
  Date: March 2008
 
  This Example assumes you want to create a class for the following mysql DB table structure,
  but you should be able to tweak this example easily to fit whatever table structure you have.
 
   CREATE TABLE `userTable` (
    `user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
    `user_name` VARCHAR( 255 ) NOT NULL ,
    `user_email` VARCHAR( 255 ) NOT NULL ,
    `user_phone` VARCHAR( 255 ) NOT NULL ,
    `user_age` INT NOT NULL ,
    `join_date` DATETIME NOT NULL ,
    PRIMARY KEY ( `user_id` )
    );
  
 */

// Include the class
require("classGenerator.class.php");


/*
 Instantiate the class, passing in the name of the class you want to generate
 along with the name of the table you are creating the class for
*/
$class = new ClassGenerator("Example","userTable");

/*
  Add in the primary key field for this table, the fields are
  $fieldName - the name of the DB Field
  $displayName - what you want the name to be in your class
  $dataType - Optional, sets the type in the PHPDoc comments for you
  $keyField - Optional, set to true on your primary key field
*/
$class->addDataMember("user_id","userId","int",true);


/*
  Add in all other additional fields in your table
*/
$class->addDataMember("user_name","username","string");
$class->addDataMember("user_email","email","string");
$class->addDataMember("user_phone","phone","string");
$class->addDataMember("user_age","age","int");

/*
  If you set the $dataType parameter to datetime, date, time, or timestamp it will
  create the getter method for this field slightly differently...check the output for this field.
*/
$class->addDataMember("join_date","joinDate","datetime");


// Generate the class!
if (!$class->generateClass())
    echo
"<br/>Failed!";
else
    echo
"Success!";