Tải bản đầy đủ (.pdf) (5 trang)

PHP and MySQL Web Development - P31 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (104.96 KB, 5 trang )

5
Reusing Code and Writing
Functions
THIS CHAPTER EXPLAINS HOW REUSING CODE
leads to more consistent, reliable, main-
tainable code, with less effort.We will demonstrate techniques for modularizing and
reusing code, beginning with the simple use of
require() and include() to use the
same code on more than one page.We will explain why these are superior to server side
includes.The example given will cover using include files to get a consistent look and
feel across your site.
We will explain how to write and call your own functions using page and form gen-
eration functions as examples.
In this chapter, we will cover
n
Reusing code
n
Using require() and include()
n
Introducing functions
n
Defining functions
n
Parameters
n
Returning values
n
Call-by-Reference versus Call-by-Value
n
Scope
n


Recursion
Why Reuse Code?
One of the goals of software engineers is to reuse code in lieu of writing new code.This
is not because software engineers are a particularly lazy group. Reusing existing code
reduces costs, increases reliability, and improves consistency. Ideally, a new project is
07 525x ch05 1/24/03 3:36 PM Page 117
118
Chapter 5 Reusing Code and Writing Functions
created by combining existing reusable components, with a minimum of development
from scratch.
Cost
Over the useful life of a piece of software, significantly more time will be spent main-
taining, modifying, testing, and documenting it than was originally spent writing it. If
you are writing commercial code, you should be attempting to limit the number of lines
that are in use within the organization. One of the most practical ways to achieve this is
to reuse code already in use rather than writing a slightly different version of the same
code for a new task. Less code means lower costs. If software exists that meets the
requirements of the new project, acquire it.The cost of buying existing software is
almost always less than the cost of developing an equivalent product.Tread carefully
though if there is existing software that almost meets your requirements. It can be more
difficult to modify existing code than to write new code.
Reliability
If a module of code is in use somewhere in your organization, it has presumably already
been thoroughly tested. Even if it is only a few lines, there is a possibility that if you
rewrite it, you will either overlook something that the original author incorporated or
something that was added to the original code after a defect was found during testing.
Existing, mature code is usually more reliable than fresh,“green” code.
Consistency
The external interfaces to your system, including both user interfaces and interfaces to
outside systems, should be consistent. It takes a will and a deliberate effort to write new

code that is consistent with the way other parts of the system function. If you are reusing
code that runs another part of the system, your functionality should automatically be
consistent.
On top of these advantages, reusing code is less work for you, as long as the original
code was modular and well written.While you work, try to recognize sections of your
code that you might be able to call on again in the future.
Using require() and include()
PHP provides two very simple, yet very useful, statements to allow you to reuse any type
of code. Using a require() or include() statement, you can load a file into your PHP
script.The file can contain anything you would normally type in a script including PHP
statements, text, HTML tags, PHP functions, or PHP classes.
These statements work similarly to the Server Side Includes offered by many Web
servers and #include statements in C or C++.
07 525x ch05 1/24/03 3:36 PM Page 118
119
Using require() and include()
Using require()
The following code is stored in a file named reusable.php:
<?php
echo 'Here is a very simple PHP statement.<br />';
?>
The following code is stored in a file called main.php:
<?php
echo 'This is the main file.<br />';
require( 'reusable.php' );
echo 'The script will end now.<br />';
?>
If you load reusable.php, it probably won’t surprise you when “Here is a very sim-
ple PHP statement.” appears in your browser. If you load main.php, something a little
more interesting happens.The output of this script is shown in Figure 5.1.

Figure 5.1 The output of main.php shows the result
of the require() statement.
A file is needed to use a require() statement. In the preceding example, we are using
the file named reusable.php.When we run our script, the require() statement
require( 'reusable.php' );
is replaced by the contents of the requested file, and the script is then executed.This
means that when we load main.php, it runs as though the script were written as follows:
<?php
echo 'This is the main file.<br />';
echo 'Here is a very simple PHP statement.<br />';
echo 'The script will end now.<br />';
?>
07 525x ch05 1/24/03 3:36 PM Page 119
120
Chapter 5 Reusing Code and Writing Functions
When using require() you need to note the different ways that filename extensions
and PHP tags are handled.
File Name Extensions and require()
PHP does not look at the filename extension on the required file.This means that you
can name your file whatever you choose as long as you’re not going to call it directly.
When you use require() to load the file, it will effectively become part of a PHP file
and be executed as such.
Normally, PHP statements would not be processed if they were in a file called for
example, page.html. PHP is usually only called upon to parse files with defined exten-
sions such as
.php.However, if you load this page.html via a require() statement, any
PHP inside it will be processed.Therefore, you can use any extension you prefer for
include files, but it would be a good idea to try to stick to a sensible convention, such as
.inc.
One thing to be aware of is that if files ending in .inc or some other non-standard

extension are stored in the Web document tree and users directly load them in the
browser, they will be able to see the code in plain text, including any passwords. It is
therefore important to either store included files outside the document tree, or use the
standard extensions.
PHP Tags and require()
In our example our reusable file (reusable.php) was written as follows:
<?php
echo 'Here is a very simple PHP statement.<br />';
?>
We placed the PHP code within the file in PHP tags.You will need to do this if you
want PHP code within a required file treated as PHP code. If you do not open a PHP
tag, your code will just be treated as text or HTML and will not be executed.
Using require() for Web Site Templates
If your company has a consistent look and feel to pages on the Web site, you can use
PHP to add the template and standard elements to pages using require().
For example, the Web site of fictional company TLA Consulting has a number of
pages all with the look and feel shown in Figure 5.2.When a new page is needed, the
developer can open an existing page, cut out the existing text from the middle of the
file, enter new text and save the file under a new name.
07 525x ch05 1/24/03 3:36 PM Page 120
121
Using require() for Web Site Templates
Figure 5.2 TLA Consulting has a standard look
and feel for all their Web pages.
Consider this scenario:The Web site has been around for a while, and there are now
tens, hundreds, or maybe even thousands of pages all following a common style. A deci-
sion is made to change part of the standard look—it might be something minor, like
adding an email address to the footer of each page or adding a single new entry to the
navigation menu. Do you want to make that minor change on tens, hundreds, or even
thousands of pages?

Directly reusing the sections of HTML that are common to all pages is a much better
approach than cutting and pasting on tens, hundreds, or even thousands of pages.
The source code for the homepage (home.html) shown in Figure 5.2 is given in Listing
5.1.
Listing 5.1 home.html—The HTML That Produces TLA Consulting’s Home Page
<html>
<head>
<title>TLA Consulting Pty Ltd</title>
<style>
h1 {color:white; font-size:24pt; text-align:center;
font-family:arial,sans-serif}
.menu {color:white; font-size:12pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold}
td {background:black}
07 525x ch05 1/24/03 3:36 PM Page 121

×