User and have three subclasses: GuestUser, CustomerUser , and AdminUser . The base class, User, and its subclasses would contain methods that give information about the user (for example, permissions on what they can access on the website and their personal preferences). So, this is the moment when the factory pattern design pattern gets involved: the elegant way for you to write your Web application is to use the base class User as much as possible, so that the code would be generic and that it would be easy to add additional kinds of users when the need arises.
//Creating the RandomMessage class containing a function to randomize the
//messages and colors arrays and two functions that returns the results
class RandomMessage {
var $messages;
var $colors;
function RandomMessage() {
// Declaring the array of messages
$this->messages=array(
'This is a factory pattern application!', 'The first application
of this article!'
);
// Declaring the array of colors
$this->colors=array(
'#FF33CC','#D6008F'
);
// Shuffle the messages
srand ((float)microtime()*1000000);
shuffle ($this->messages);
// Shuffle the colors
srand ((float)microtime()*1000000);
shuffle ($this->colors);
}
//Return the randomized message
function getMessage() {
return $this->messages[0];
}
//Return the randomize color
function getColor() {
return $this->colors[0];
}
}
//Defining the factory class that contains the factory method
class messageCreator {
// The factory method...
function & createMessage () {
// Return a new instance of RandomMessage class
return new RandomMessage();
}
}
//Creating an instance of the factory method, messageCreator, from above
$messageMaker=new messageCreator;
//Outputs the result
for ( $i=0; $i<5; $i++ )
{
$randomMessage=& $messageMaker->createMessage();
$color = $randomMessage->getColor();
echo ( ''.$randomMessage->
getMessage().'
' );
}
?>
This is a factory pattern application!
The first application of this article!
This is a factory pattern application!
The first application of this article!
The first application of this article!
This is a factory pattern application!
This is a factory pattern application!
This is a factory pattern application!
The first application of this article!
This is a factory pattern application!
makeFlower method listed below:
function & makeFlower () {
// Return a new instance of RandomMessage
return new flower();
}
echo "";
echo "";
echo "";
echo "";
echo "
FLOWERS
";
//defining the base class
class flower {
var $flowers;
function flower() {
// An array of messages
$this->flowers=array(
'Tulips','Roses', 'Lilies'
);
// Shuffle the messages
srand ((float)microtime()*1000000);
shuffle ($this->flowers);
}
function getMessage() {
return $this->flowers[0];
}
}
//defining the factory class
class FlowersFactory {
// The factory method…
function & makeFlower () {
// Return a new instance of RandomMessage
return new flower();
}
}
//Creating an instance of the factory method, FlowersFactory, from above
$flowersFactory=new flowersFactory;
//Outputs the result
for ( $i=0; $i<5; $i++ ) {
$randomFlower=& $flowersFactory->makeFlower();
echo ( $randomFlower->getMessage().' ' );
$picture = $randomFlower->getMessage();
echo ' ';
}
echo "";
echo "";
?>
insert into bookstore values (1,'PHP and MySQL','Welling Thomson','234-2445-1234','23$')
insert into bookstore values (2,'PHP5','Addison Wesley','189-210-123','19$')
insert into bookstore values (3,'JavaServerPages','SAMS','355-677-888','31$')
insert into bookstore values (4,'Visual Basic.net','JAMSA','611-115-564','26$')
insert into bookstore values (5,'JBoss Tools 3','Anghel Leonard','433-366-564','39$')
getArticle, and returns all the records: id, book, author, isbn, price content, one at time.
// Fetches a list of articles from a database
class Articles {
var $articles;
function Articles ( &$dbConn ) {
// Pull the articles from the database
$sql="SELECT * FROM bookstore";
$result = $dbConn->query($sql);
// Storing the articles into the articles array
while ( $row = mysqli_fetch_array($result) ) {
$this->articles[]=$row;
}
}
// Factory method creates new instances of Article
function & getArticle () {
foreach ($this->articles as $row){
$instance[] = new Article($row);
}
return $instance;
}
}
// The Article class is built by the factory method
class Article {
var $id;
var $book;
var $author;
var $isbn;
var $price;
function Article($data) {
$this->id=$data['id'];
$this->book=$data['book'];
$this->author=$data['author'];
$this->isbn=$data['isbn'];
$this->price=$data['price'];
}
function id() {
return htmlspecialchars($this->id);
}
function book() {
return htmlspecialchars($this->book);
}
function author() {
return htmlspecialchars($this->author);
}
function isbn() {
return htmlspecialchars($this->isbn);
}
function price() {
return htmlspecialchars($this->price);
}
}
const MYSQL_HOST = "127.0.0.1";
const MYSQL_USER = "";
const MYSQL_PASS = "";
const MYSQL_DB = "books";
//Connecting to MySQL
$dbConn = new MySQLi(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
if($dbConn->connect_error)
{
throw new Exception('MySQL connection failed: ' . $dbConn-> connect_error);
} else {
//creating an instance of the Articles class
$dbarticles=new Articles($dbConn);
// The factory method is used here
$articleInstances=$dbarticles->getArticle();
//Outputs each record fields
foreach ($articleInstances as $article){
echo ( "
".$article->id()."
________________________________________
" );
echo ( "".$article->book()."
" );
echo ( "".$article->author()."
" );
echo ( "".$article->isbn()."
" );
echo ( "".$article->price()."
" );
}
}
//Close the connection
mysql_close($dbConn);
?>
// Fetches a list of articles from a database
class Articles {
var $articles;
function Articles ( &$dbConn ) {
// Pull the articles from the database
$sql="SELECT * FROM bookstore";
$result = $dbConn->query($sql);
// Storing the articles into the articles array
while ( $row = mysqli_fetch_array($result) ) {
$this->articles[]=$row;
}
}
// Factory method creates new instances of Article
function & getArticle () {
foreach ($this->articles as $row){
$instance[] = new Article($row);
}
return $instance;
}
}
// The Article class is built by the factory method
class Article {
var $id;
var $book;
var $author;
var $isbn;
var $price;
function Article($data) {
$this->id=$data['id'];
$this->book=$data['book'];
$this->author=$data['author'];
$this->isbn=$data['isbn'];
$this->price=$data['price'];
}
function id() {
return htmlspecialchars($this->id);
}
function book() {
return htmlspecialchars($this->book);
}
function author() {
return htmlspecialchars($this->author);
}
function isbn() {
return htmlspecialchars($this->isbn);
}
function price() {
return htmlspecialchars($this->price);
}
}
const MYSQL_HOST = "127.0.0.1";
const MYSQL_USER = "";
const MYSQL_PASS = "";
const MYSQL_DB = "books";
//Connecting to MySQL
$dbConn = new MySQLi(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
if($dbConn->connect_error)
{
throw new Exception('MySQL connection failed: ' . $dbConn-> connect_error);
} else {
//creating an instance of the Articles class
$dbarticles=new Articles($dbConn);
// The factory method is used here
$articleInstances=$dbarticles->getArticle();
//Outputs each record fields
foreach ($articleInstances as $article){
echo ( "
".$article->id()."
" );
echo ( "".$article->book()."
" );
echo ( "".$article->author()."
" );
echo ( "".$article->isbn()."
" );
echo ( "".$article->price()."
" );
}
}
//Close the connection
mysql_close($dbConn);
?>
The output of the database.php application is listed below:
PHP and MySQL
Welling Thomson
234-2445-1234
23$
PHP5
Addison Wesley
189-210-123
19$
JavaserverPages
SAMS
355-677-888
31$
Visual Basic.net
JAMSA
611-115-564
26$
JBoss Tools 3
Anghel Leonard
433-366-564
39$