13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

134 Chapter 5 Reusing Code <strong>and</strong> Writing Functions<br />

Cost<br />

Over the useful life of a piece of software, significantly more time will be spent maintaining,<br />

modifying, testing, <strong>and</strong> documenting it than was originally spent writing it. If<br />

you are writing commercial code, you should attempt to limit the number of lines in use<br />

within the organization. One of the most practical ways to achieve this goal is to reuse<br />

code already in use instead of writing a slightly different version of the same code for a<br />

new task. Less code means lower costs. If existing software meets the requirements of the<br />

new project, acquire it.The cost of buying existing software is almost always less than the<br />

cost of developing an equivalent product.Tread carefully, though, if existing software<br />

almost meets your requirements. Modifying existing code can be more difficult than<br />

writing new code.<br />

Reliability<br />

If a module of code is in use somewhere in your organization, it has presumably already<br />

been thoroughly tested. Even if this module contains only a few lines, there is a possibility<br />

that, if you rewrite it, you will either overlook something that the original author<br />

incorporated or something that was added to the original code after a defect was found<br />

during testing. Existing, mature code is usually more reliable than fresh,“green” code.<br />

Consistency<br />

The external interfaces to your system, including both user interfaces <strong>and</strong> interfaces to<br />

outside systems, should be consistent.Writing new code consistent with the way other<br />

parts of the system function takes a will <strong>and</strong> a deliberate effort. If you are reusing code<br />

that runs another part of the system, your functionality should automatically be<br />

consistent.<br />

On top of these advantages, reusing code is less work for you, as long as the original<br />

code was modular <strong>and</strong> well written.While you work, try to recognize sections of your<br />

code that you might be able to call on again in the future.<br />

Using require() <strong>and</strong> include()<br />

<strong>PHP</strong> provides two very simple, yet very useful, statements to allow you to reuse any type<br />

of code. Using a require() or include() statement, you can load a file into your <strong>PHP</strong><br />

script.The file can contain anything you would normally type in a script including <strong>PHP</strong><br />

statements, text, HTML tags, <strong>PHP</strong> functions, or <strong>PHP</strong> classes.<br />

These statements work similarly to the server-side includes offered by many web<br />

servers <strong>and</strong> #include statements in C or C++.<br />

The statements require() <strong>and</strong> include() are almost identical.The only difference<br />

between them is that when they fail, the require() construct gives a fatal error, whereas<br />

the include() construct gives only a warning.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!