GCarrillo A Framework For The Development of Web Applications in PHP
GCarrillo A Framework For The Development of Web Applications in PHP
Gabriel Carrillo
UNELLEZ-Apure, San Fernando 7001, Apure, Venezuela.
[email protected], (+58 412) 6818804
ABSTRACT
The objective of this research is to build a Framework to develop Web applications in
PHP language. A Framework is a methodology that provides basic structure and
functions to develop software. In this proposal, the principle of separation of logic,
content and presentation is applied, and for this reason the Model View Controller has
been adopted. The result of this research is an agile methodology for the
development of Web applications. It is based on object oriented PHP, design patterns
and MySQL database. It can be used by students and professionals to develop
application software. This Framework is extensible, fine grained with loosely coupled
components.
Key Words: Web applications development, PHP Framework, Model View Controller.
INTRODUCTION
The development of Web applications is a hard task, and developing reusable
quality software is even harder (Gamma, Helm, Johnson & Vlissides, 1994, p.1). This
is even more evident, if we consider the shortage of software developers in many
countries (Latin America, Europe).
Developing Web applications requires a greater amount of elements and
technology to consider, in comparison to desktop applications. A wide range of
languages, methodologies and technologies for developing software are available to
students and novice programmers. Nevertheless, at early stages few programmers
manage to assemble properly the available elements with good software
development practices.
In the Software Engineering industry, this problem has been recognized since
the 70’s. Engineers and researchers have attempted to change software development
into a systematic discipline, with the characteristics of Engineering. Over the years we
have seen: Structured design and programming, Object oriented programming, UML,
Rational Unified process, design patters, agile methodologies. One of the problems is
that traditional process of Web Engineering does not work properly because the
development of Web systems differs from traditional software development (Eldai, Ali
& Raviraja, 2008, p. 190).
The main objective of this research is to create a Framework to develop Web
system in PHP. This Framework will provide a methodological support for agile
development of applications.
METHODS
A Framework is a set of components that collaborate is to provide a reusable
architecture for a family of related applications (Schmidt, 2004, p.3). It allows the
reuse of detailed design and code, and because it is a proven solution for a type of
problem, it facilitates the construction of quality software.
The PHP Framework uses a set of elements and principles of Software
Engineering:
‒ Object Oriented Programming in PHP.
‒ Design patterns: Singleton, Chain of responsibility (Eldai, Ali, and Raviraja,
2008).
‒ Architectural pattern: Model View Controller.
‒ Elements are loosely coupled.
‒ Convention over configuration: The path of some files and some names are
preset.
‒ Principles of open / closure: Components are open to extension and closed to
modification.
RESULTS
In this research, a Framework for developing Web systems in PHP is
proposed. The elements of this Framework: Architectural pattern, design pattern,
directory structure, control flow, controllers, coding rules, database and views.
Architectural pattern
In figure 1 the Model View Controller (MVC) architectural pattern is depicted. In
a Web application, all requests are handled through a controller. This element is in
charge of handling the main logic and control flow of the application. When a
controller requires data or business logic, it invokes a model, and this returns the
necessary data. All database operations must be performed through a model. A
model can be created to provide business functions. The view is part in charge of
displaying information to users, that is, it is the visible part and it allows the interaction
user-system.
Directory structure
The directory structure is depicted in figure 2.
Control flow
The file index.php is the only Access point to the application. Index.php is in the
application folder (app_folder in figure 2). The logic of the application is performed in
the controllers, so it is necessary that index.php route every request to the proper
controller. This is accomplished by applying the chain of responsibility design pattern.
The control flow of an application is performed according to the following steps:
1. An user sends a request to the front controller (index.php).
2. The front controller selects (routes) the controller and action of the request.
3. The controller request data to the model.
4. The model returns results to the controller.
5. The controller passes data to the view, and the view is sent to the user.
Coding rules
Format of PHP files:
‒ Indent 4 blanks in while, for, if, switch blocks.
‒ Keep lines within 80 characters.
File names
‒ Use only letters, numbers and underscore. Do not use blanks.
‒ Files have an extension related to function: php, js, css.
Variable names
‒ Variable names must be related to context. They begin with symbol $ y lower case
letters. Do not use ñ or special characters. Example: $salary, $name.
‒ Use camel case for long names: Example: $birthDate, $basicSalary,
$grossYearlySalary.
‒ Avoid using too short names, such as $i, $n. they should used only as indexes in
simple expressions.
Classes
‒ The name of a class begins with capital letters. Example: class Client.
‒ Classes have a documentation block. Example:
‒ One class per file.
‒ Indent 4 blank spaces.
Design pattern
The Singleton pattern is used for dtabase conection:
class BD
{
static private $instance = NULL;
function __construct()
{
$this->idConn = mysql_connect("localhost", "user","pass");
mysql_select_db("nombrebd",$this->idConn);
}
<?php
/**
* File : index.php
* Function : Front controller of the application
*
*/
if(! empty($_GET['act'])) {
$actionName = $_GET['act'];
} else {
$actionName = "main";
}
require $controllerPath;
Views
Views are written in php files. A view has no logic, only html code and
embedded php to show variables values. Results of database operations are passed
as associative arrays to the view.
Controllers
The main logic and control of the application is performed in controllers. They
are classes with methods for every defined action in the system. In figure 5, a
controller for class employee is depicted.
<?php
/**
* Archivo : Employee.class.php
*/
class Employee
{
function main() {
// Auto load
require "inc/autoload.inc.php";
$data["item"] = $item;
$oOutput = new View("employee.view.php",$data);
}
}
?>
Figure 5. A controller.
Data base
All database operations are performed by means of classes and methods.
<?php
/**
* file : modelEmployee.php
*/
class modelEmployee
{
function __construct()
{
$this->idConn = BD::getInstance()->idConn;
}
function getAll()
{
$sql = "SELECT * FROM employee";
$resultSet = mysql_query($sql,$this->idConn);
return $resultSet;
}
}
?>
REFERENCES
Eldai, O.I., Ali, A. y Raviraja, S. (2008). Towards a New Methodology for Developing
Web-Based Systems. World Academy of Science, Engineering and Technology
46 2008 [On line]. Available: http://www.waset.org/journals/waset/v46/v46-
35.pdf.
Gamma, E., Helm, R., Johnson, R. y Vlissides, J. (1994). Design Patterns: Elements
of Reusable Object-Oriented Software. Boston: Addison-Wesley.
Schmidt, D. (2004). Introduction to Patterns and Frameworks. [On line]. Available:
http://www.cs.wustl.edu/~schmidt/PDF/patterns-intro4.pdf.