PHP 5: Power Programming (Open Source Series)

Overview

"This book, written by my colleague, Andi Gutmans, and two very prominent PHP developers, Stig Bakken and Derick Rethans, holds the key to unlocking the riches of PHP 5. It thoroughly covers all of the features of this new version, and is a must have for all PHP developers interested in exploring PHP 5's advanced features"

—Zeev Suraski, Co-Designer of PHP 5 and Co-Creator of the Zend Engine

  • Expert PHP 5 programming techniques, direct from PHP 5 co-creator and key developers
  • ...
See more details below
Paperback
$38.93
BN.com price
(Save 20%)$48.99 List Price
Other sellers (Paperback)
  • All (16) from $1.99   
  • New (6) from $34.02   
  • Used (10) from $1.99   
Sending request ...

Overview

"This book, written by my colleague, Andi Gutmans, and two very prominent PHP developers, Stig Bakken and Derick Rethans, holds the key to unlocking the riches of PHP 5. It thoroughly covers all of the features of this new version, and is a must have for all PHP developers interested in exploring PHP 5's advanced features"

—Zeev Suraski, Co-Designer of PHP 5 and Co-Creator of the Zend Engine

  • Expert PHP 5 programming techniques, direct from PHP 5 co-creator and key developers
  • Definitive coverage of the PHP object model, design patterns, XML/SOAP integration, and more
  • Solid introduction and expert insights into performance tuning, PEAR, extensions, and database access
  • The definitive introduction to PHP 5's advanced features!
  • The definitive introduction for today's most powerful PHP 5 techniques

In this book, PHP 5's co-creator and two leading PHP developers show you how to make the most of PHP 5's industrial-strength enhancements in any project—no matter how large or complex. Their unique insights and realistic examples illuminate PHP 5's new object model, powerful design patterns, improved XML Web services support, and much more. Whether you're creating web applications, extensions, packages, or shell scripts—or migrating PHP 4 code—here are high-powered solutions you won't find anywhere else.

Review PHP's syntax and master its object-oriented capabilities—from properties and methods to polymorphism, interfaces, and reflection

  • Master the four most important design patterns for PHP development
  • Write powerful web applications: handle input, cookies, session extension, and more
  • Integrate with MySQL, SQLite, and other database engines
  • Provide efficient error handling that's transparent to your users
  • Leverage PHP 5's improved XML support—including parsing, XSLT conversions, and more
  • Build XML-based web services with XML-RPC and SOAP
  • Make the most of PEAR: work with the repository, use key packages, and create your own
  • Upgrade PHP 4 code to PHP 5—compatibility issues, techniques, and practical workarounds
  • Improve script performance: tips and tools for PHP optimization
  • Use PHP extensions to handle files/streams, regular expressions, dates/times, and graphics
  • Create original extensions and shell scripts

If you're a software developer new to PHP, you'll leap quickly into PHP and its new object-oriented capabilities. If you're an experienced PHP programmer, you already recognize PHP's convenience and simplicity. Now, discover all of its extraordinary power!

ABOUT THE SOFTWARE

At this book's link to Zend.com, download a fully functional 90-day version of Zend Studio, today's breakthrough tool for developing, managing, and debugging PHP code.

Series Editor Bruce Perens' is an open source evangelist, developer, and consultant whose software is a major component of most commercial embedded Linux offerings. He founded or cofounded Linux Standard Base, Open Source Initiative, and Software in the Public Interest. As Debian GNU/Linux Project Leader, he was instrumental in getting the system on two U.S. space shuttle flights.

Read More Show Less

Editorial Reviews

Slashdot.org
The authors succeed in providing an excellent manual for "power programming" in PHP 5. There is terrific guidance here for many PHP developers and experienced Java, C++ and C# coders who are looking to either migrate to PHP 5 from v.4 or who are looking to gain experience in PHP programming. The experience the authors bring to the table is indisputable and their style of writing and the ease with which they bring new language and tools to their audience is admirable.
Read More Show Less

Product Details

  • ISBN-13: 9780131471498
  • Publisher: Prentice Hall
  • Publication date: 10/19/2004
  • Series: Bruce Perens' Open Source Series
  • Pages: 720
  • Product dimensions: 6.85 (w) x 9.21 (h) x 1.49 (d)

Meet the Author

ANDI GUTMANS co-created PHP 3 and all versions since. A member of the PHP core team, he is one of the world¿s most highly regarded PHP developers.

STIG SÆTHER BAKKEN, also a member of the PHP core team, created PEAR: PHP Extension and Application Repository, the framework and global distribution system for reusable PHP components. Gutmans and Bakken are members of the PHP Hall of Fame.

DERICK RETHANS is a PHP code contributor and leader of the PHP QA team.

Read More Show Less

Read an Excerpt

PrefacePreface

"The best security against revolution is in constant correction of abuses and the introduction of needed improvements. It is the neglect of timely repair that makes rebuilding necessary."—Richard Whately

In the Beginning

It was eight years ago, when Rasmus Lerdorf first started developing PHP/FI. He could not have imagined that his creation would eventually lead to the development of PHP as we know it today, which is being used by millions of people. The first version of "PHP/FI," called Personal Homepage Tools/Form Interpreter, was a collection of Perl scripts in 1995.1 One of the basic features was a Perl-like language for handling form submissions, but it lacked many common useful language features, such as loops.

PHP/FI 2

A rewrite came with PHP/FI 22 in 1997, but at that time the development was almost solely handled by Rasmus. After its release in November of that year, Andi Gutmans and Zeev Suraski bumped into PHP/FI while looking for a language to develop an e-commerce solution as a university project. They discovered that PHP/FI was not quite as powerful as it seemed, and its language was lacking many common features. One of the most interesting aspects included the way loops were implemented. The hand-crafted lexical scanner would go through the script and when it hit the while keyword it would remember its position in the file. At the end of the loop, the file pointer sought back to the saved position, and the whole loop was reread and re-executed.

PHP 3

Zeev and Andi decided to completely rewrite the scripting language. They then teamed up with Rasmus to release PHP 3, and along also came a new name: PHP: Hypertext Preprocessor, to emphasize that PHP was a different product and not only suitable for personal use. Zeev and Andi had also designed and implemented a new extension API. This new API made it possible to easily support additional extensions for performing tasks such as accessing databases, spell checkers and other technologies, which attracted many developers who were not part of the "core" group to join and contribute to the PHP project. At the time of PHP 3's release3 in June 1998, the estimated PHP installed base consisted of about 50,000 domains. PHP 3 sparked the beginning of PHP's real breakthrough, and was the first version to have an installed base of more than one million domains.

PHP 4

In late 1998, Zeev and Andi looked back at their work in PHP 3 and felt they could have written the scripting language even better, so they started yet another rewrite. While PHP 3 still continuously parsed the scripts while executing them, PHP 4 came with a new paradigm of "compile first, execute later." The compilation step does not compile PHP scripts into machine code; it instead compiles them into byte code, which is then executed by the Zend Engine (Zend stands for Zeev & Andi), the new heart of PHP 4. Because of this new way of executing scripts, the performance of PHP 4 was much better than that of PHP 3, with only a small amount of backward compatibility breakage4. Among other improvements was an improved extension API for better run-time performance, a web server abstraction layer allowing PHP 4 to run on most popular web servers, and lots more. PHP 4 was officially released on May 22, 2002, and today its installed base has surpassed 15 million domains.

In PHP 3, the minor version number (the middle digit) was never used, and all versions were numbered as 3.0.x. This changed in PHP 4, and the minor version number was used to denote important changes in the language. The first important change came in PHP 4.1.0,5 which introduced superglobals such as and . Superglobals can be accessed from within functions without having to use the keyword. This feature was added in order to allow the register_globals INI option to be turned off. register_globals is a feature in PHP which automatically converts input variables like "?foo=bar" in http://php.net/?foo=bar to a PHP variable called $foo. Because many people do not check input variables properly, many applications had security holes, which made it quite easy to circumvent security and authentication code.

With the new superglobals in place, on April 22, 2002, PHP 4.2.0 was released with the register_globals turned off by default. PHP 4.3.0, the last significant PHP 4 version, was released on December 27, 2002. This version introduced the Command Line Interface (CLI), a revamped file and network I/O layer (called streams), and a bundled GD library. Although most of those additions have no real effect on end users, the major version was bumped due to the major changes in PHP's core.

PHP 5

Soon after, the demand for more common object-oriented features increased immensely, and Andi came up with the idea of rewriting the objected-oriented part of the Zend Engine. Zeev and Andi wrote the "Zend Engine II: Feature Overview and Design" document6 and jumpstarted heated discussions about PHP's future. Although the basic language has stayed the same, many features were added, dropped, and changed by the time PHP 5 matured. For example, namespaces and multiple inheritance, which were mentioned in the original document, never made it into PHP 5. Multiple inheritance was dropped in favor of interfaces, and namespaces were dropped completely. You can find a full list of new features in Chapter, "What Is New in PHP 5?"

PHP 5 is expected to maintain and even increase PHP's leadership in the web development market. Not only does it revolutionizes PHP's object-oriented support but it also contains many new features which make it the ultimate web development platform. The rewritten

Audience

This book is an introduction to the advanced features new to PHP 5. It is written for PHP programmers who are making the move to PHP 5. Although Chapter 2, "PHP 5 Basic Language," contains an introduction to PHP 5 syntax, it is meant as a refresher for PHP programmers and not as a tutorial for new programmers. However, web developers with experience programming other high-level languages may indeed find that this tutorial is all they need in order to begin working effectively with PHP 5.

Chapter Overview

Chapter 1, "What Is New in PHP 5?" discusses the new features in PHP 5. Most of these new features deal with new object-oriented features, including small examples for each feature. It also gives an overview of the new extensions in PHP 5. Most of the topics mentioned in this chapter are explained in more detail in later chapters.

Chapter 2, "PHP 5 Basic Language," introduces the PHP syntax to those readers not familiar with PHP. All basic language constructs and variable types are explained along with simple examples to give the reader the necessary building blocks to build real scripts.

Chapter 3, "PHP 5 OO Language," continues exploring PHP 5's syntax, focusing on its object-oriented functionality. This chapter covers basics, such as properties and methods, and progresses to more complicated subjects, such as polymorphism, interfaces, exceptions, and lots more.

Using the previous chapter as a foundation, Chapter 4, "PHP 5 Advanced OOP and Design Patterns," covers some of the most advanced features of PHP 5's object model. After learning these features, including four commonly used design patterns and PHP's reflection capabilities, you will soon become an OO wizard.

Now that you are familiar with the syntax and language features of PHP, Chapter 5, "How to Write a Web Application with PHP," introduces you to the world of writing web applications. The authors show you basics, such as handling input through form variables and safety techniques, but this chapter also includes more advanced topics, such as handling sessions with cookies and PHP's session extension. You also find a few tips on laying out your source code for your web applications.

Chapter 6, "Databases with PHP 5," introduces using MySQL, SQLite, and Oracle from PHP, but focuses primarily on the PHP 5-specific details of database access. For each database, you learn about some of its strong and weak points, as well as the types of applications at which each excels. And of course, you learn how to interface with them using PHP's native functions or using PEAR DB.

All scripts can throw errors, but of course you do not want them to show up on your web site once your application has passed its development state. Chapter 7, "Error Handling," deals with different types of errors that exist, how to handle those errors with PHP, and how to handle errors with PEAR.

As one of the important new features in PHP 5 is its renewed

Although not specifically for PHP 5, the five mainstream extensions that Chapter 9,"Mainstream Extensions," covers are important enough to deserve a place in this book. The first section, "Files and Streams," explains about handling files and network streams. A stream is nothing more than a way to access external data, such as a file, remote URL, or compressed file. The second section, "Regular Expressions," explains the syntax of a regular expression engine (PCRE) that PHP uses with numerous examples to show you how these expressions can make your life easier. In "Date Handling," we explain the different functions used to parse and format date and time strings. In "Graphics Manipulation with GD," we show you through two real-life scenarios the basic functions of creating and manipulating graphics with PHP. The last section in this chapter, "Multibyte Strings and Character Sets," explains the different character sets and the functions to convert and handle different ones, including multi-byte strings used in Asian languages.

Chapter 10, "Using PEAR," introduces PEAR, the PHP Extension and Application Repository. Starting with concepts and installation, the chapter shows how to use PEAR and maintain the local installed packages. This chapter also includes a tour of the PEAR web site.

Chapter 11, "Important PEAR Packages," gives an overview of the most important PEAR packages, along with examples. Packages covered include Template Systems, the Auth package to do authentication, form handling with the HTML_QuickForm package, and a package used to simplify caching.

Chapter 12, "Building PEAR Components," explains how to create your own PEAR package. The PEAR Coding Standard and package.

Chapter 13, "Making the Move," deals with the few backward-incompatible changes that were introduced between PHP 4 and PHP 5. This chapter tells you which things you need to take care of when making your application work on PHP 5, and provides workarounds wherever possible.

Chapter 14, "Performance," shows you how to make your scripts perform better. The chapter offers tips on standard PHP usage, the use of external utilities (APD and Xdebug) to find problems in your scripts, and PHP accelerators like APC and Zend Performance Suite.

Chapter 15, "An Introduction to Writing PHP Extensions," explains how to write your own custom PHP extension. We use a simple example to explain the most important things like parameter parsing and resource management.

Chapter 16, "PHP Shell Scripting," shows you how to write shell scripts in PHP, because PHP is useful for more than just web applications. We carefully explain the differences between the CLI and CGI executables in which PHP comes, including command-line parameter parsing and process control.

This book also includes three appendices. Appendix A, "PEAR and PECL Package Index," provides an overview of all important packages, with descriptions and dependencies on other packages. Appendix B, "phpDocument Format Reference," explains the syntax as understood by the PHP Documenter tool to generate API documentation from source code. Appendix C, "Zend Studio Quick Start," is an introduction to working in the Zend Studio IDE.

A Note About Coding Styles

There are almost as many coding styles as there are programmers. The PHP examples in this book follow the PEAR coding standard, with the opening curly bracket on the line below the function name. In some cases, we've placed the curly bracket on the same line as the function name. We encourage you to adopt the style you are most comfortable with.

Note - A code continuation character, , appears at the beginning of code lines that have wrapped down from the line above it.

About the Software

Included in the back of this book is a special link to Zend.com, where you can download a fully functional, 90-day trial version of the Zend Studio IDE. Be sure to use the license key printed on the inside back cover of this book when you install Zend Studio.

The Zend Development Environment (ZDE) is a convenient tool that integrates an editor, debugger, and project manager to help you develop, manage, and debug your code. It can connect to your own installed server or directly to the Zend Studio server component. It is a powerful tool that allows you to debug your code in its natural environment.

Updates and Errata and Downloads

Updates, errata, and copies of the sample programs used in this book can be found at the following URL: http//php5powerprogramming.com. We encourage you to visit this site.

Acknowledgements

This book could not have been written without feedback from our technical reviewers; therefore, we would like to thank Marcus B_ger, Steph Fox, Martin Jansen, and Rob Richards for their excellent comments and feedback. Besides these four reviewers, there are a few more people who helped answer several questions during the writing of this book, more specifically Christian Stocker for helping with the

We would also like to thank Mark L. Taub and the editorial team of Pearson PTR for the things they are good at doing: organizing, planning, and marketing this book, and making sure everything fits together. Thanks to Janet Valade, for helpful developmental editing support, and our project editor Kristy Hart, who helped us wrap up the book under pressure and put the final touches on it.

Enjoy!

Andi, Stig, and Derick

1 http://groups.google.com/groups?selm=3r7pgp$aa1@ionews.io.org.
2 http://groups.google.com/groups?selm=Dn1JM9.61t%40gpu.utcc.utoronto.ca.
3 http://groups.google.com/groups?selm=Pine.WNT.3.96.980606130654.-317675I-100000%40shell.lerdorf.on.ca.
4 http://www.php.net/manual/en/migration4.php.
5 http://www.php.net/release_4_1_0.php.
6 http://zend.com/engine2/ZendEngine-2.0.pdf.

© Copyright Pearson Education. All rights reserved.

Read More Show Less

Table of Contents

Foreword.

Preface.

1. What Is New in PHP 5?

Introduction.

Language Features.

New Object-Oriented Model.

New Object-Oriented Features.

Other New Language Features.

General PHP Changes.

XML and Web Services.

Other New Features in PHP 5.

New Memory Manager.

Dropped Support for Windows 95.

Summary.

2. PHP 5 Basic Language.

Introduction.

HTML Embedding.

Comments.

Variables.

Indirect References to Variables.

Managing Variables.

Superglobals.

Basic Data Types.

Integers.

Floating-Point Numbers.

Strings.

Booleans.

Null.

Arrays.

Constants.

Operators.

Binary Operators.

Assignment Operators.

Comparison Operators.

Logical Operators.

Bitwise Operators.

Unary Operators.

Negation Operators.

Increment/Decrement Operators.

The Cast Operators.

The Silence Operator.

The One and Only Ternary Operator.

Control Structures.

Conditional Control Structures.

Loop Control Structures.

Code Inclusion Control Structures.

Functions.

User-Defined Functions.

Function Scope.

Returning Values By Value.

Returning Values By Reference.

Declaring Function Parameters.

Static Variables.

Summary.

3. PHP 5 OO Language.

Introduction.

Objects.

Declaring a Class.

The new Keyword and Constructors.

Destructors.

Accessing Methods and Properties Using the $this Variable.

public, protected, and private Properties.

public, protected, and private Methods.

Static Properties.

Static Methods.

Class Constants.

Cloning Objects.

Polymorphism.

parent:: and self::.

instanceof Operator.

Interfaces.

Inheritance of Interfaces.

final Methods.

final Classes.

__toString() Method.

Exception Handling.

__autoload().

Class Type Hints in Function Parameters.

Summary.

4. PHP 5 Advanced OOP and Design Patterns.

Introduction.

Overloading Capabilities.

Property and Method Overloading.

Overloading the Array Access Syntax.

Iterators.

Design Patterns.

Strategy Pattern.

Singleton Pattern.

Factory Pattern.

Observer Pattern.

Reflection.

Introduction.

Reflection API.

Reflection Examples.

Implementing the Delegation Pattern Using Reflection.

Summary.

5. How to Write a Web Application with PHP.

Introduction.

Embedding into HTML.

User Input.

Safe-Handling User Input.

Common Mistakes.

Techniques to Make Scripts "Safe".

Input Validation.

HMAC Verification.

PEAR::Crypt_HMAC.

Input Filter.

Working with Passwords.

Error Handling.

Cookies.

Sessions.

Handling the Incoming Uploaded File.

Architecture.

One Script Serves All.

One Script per Function.

Separating Logic from Layout.

Summary.

6. Databases with PHP 5.

Introduction.

MySQL.

MySQL Strengths and Weaknesses.

PHP Interface.

Example Data.

Connections.

Buffered Versus Unbuffered Queries.

Queries.

Multi Statements.

Fetching Modes.

Prepared Statements.

BLOB Handling.

SQLite.

SQLite Strengths and Weaknesses.

Best Areas of Use.

PHP Interface.

PEAR DB.

Obtaining PEAR DB.

Pros and Cons of Database Abstraction.

Which Features Are Abstracted?

Database Connections.

Executing Queries.

Fetching Results.

Sequences.

Portability Features.

Abstracted Errors.

Convenience Methods.

Summary.

7. Error Handling.

Introduction.

Types of Errors.

Programming Errors.

Undefined Symbols.

Portability Errors.

PHP Errors.

PEAR Errors.

The PEAR_Error Class.

Handling PEAR Errors.

PEAR Error Modes.

Graceful Handling.

Exceptions.

What Are Exceptions?

try, catch, and throw.

Summary.

8. XML with PHP 5.

Introduction.

Vocabulary.

Parsing XML.

SAX.

DOM.

SimpleXML.

Creating a SimpleXML Object.

Browsing SimpleXML Objects.

Storing SimpleXML Objects.

PEAR.

XML_Tree.

XML_RSS.

Converting XML.

XSLT.

Communicating with XML.

XML-RPC.

SOAP.

Summary.

9. Mainstream Extensions.

Introduction.

Files and Streams.

File Access.

Program Input/Output.

Input/Output Streams.

Compression Streams.

User Streams.

URL Streams.

Locking.

Renaming and Removing Files.

Temporary Files.

Syntax.

Functions.

Date Handling.

Retrieving Date and Time Information.

Formatting Date and Time.

Parsing Date Formats.

Graphics Manipulation with GD.

Case 1: Bot-Proof Submission Forms.

Case 2: Bar Chart.

Exif.

Multi-Byte Strings and Character Sets.

Character Set Conversions.

Extra Functions Dealing with Multi-Byte Character Sets.

Locales.

Summary.

10. Using PEAR.

Introduction.

PEAR Concepts.

Packages.

Releases.

Version Numbers.

Obtaining PEAR.

Installing with UNIX / Linux PHP Distribution.

Installing with PHP Windows Installer.

go-pear.org.

Installing Packages.

Using the pear Command.

Configuration Parameters.

PEAR Commands.

pear install.

pear list.

pear info.

pear list-all.

pear list-upgrades.

pear upgrade.

pear upgrade-all.

pear uninstall.

pear search.

pear remote-list.

pear remote-info.

pear download.

pear config-get.

pear config-show.

Shortcuts.

Installer Front-Ends.

CLI (Command Line Interface) Installer.

Gtk Installer.

Summary.

11. Important PEAR Packages.

Introduction.

Database Queries.

Template Systems.

Template Terminology.

HTML_Template_IT.

HTML_Template_Flexy.

Authentication.

Overview.

Example: Auth with Password File.

Example: Auth with DB and User Data.

Auth Security Considerations.

Auth Scalability Considerations.

Auth Summary.

Form Handling.

HTML_QuickForm.

Example: Login Form.

Receiving Data.

Caching.

Cache_Lite.

Summary.

12. Building PEAR Components.

Introduction.

PEAR Standards.

Symbol Naming.

Indentation.

Release Versioning.

CLI Environment.

Fundamentals.

When and How to Include Files.

Error Handling.

Building Packages.

PEAR Example: HelloWorld.

Building the Tarball.

Verification.

Package Information.

Release Information.

Dependencies.

Element: .

Element: .

Dependency Types.

Reasons to Avoid Dependencies.

Optional Dependencies.

Some Examples.

String Substitutions.

Element: .

Examples.

Including C Code.

Element: .

Element: .

Releasing Packages.

The PEAR Release Process.

Packaging.

Source Analysis.

MD5 Checksum Generation.

Package.xml Update.

Tarball Creation.

Uploading.

Upload Release.

Finished!

Summary.

13. Making the Move.

Introduction.

The Object Model.

Passing Objects to Functions.

Compatibility Mode.

Casting Objects.

Comparing Objects.

Other Changes.

Assigning to $this.

get_class.

E_STRICT.

Automagically Creating Objects.

var and public.

Constructors.

Inherited Methods.

Define Classes Before Usage.

Command-Line Interface.

Comment Tokens.

MySQL.

Changes in Functions.

array_merge().

strrpos() and strripos().

Summary.

14. Performance.

Introduction.

Design for Performance.

PHP Design Tip #1: Beware of State.

PHP Design Tip #2: Cache!

PHP Design Tip #3: Do Not Over Design!

Benchmarking.

Using ApacheBench.

Using Siege.

Testing Versus Real Traffic.

Proling with Zend Studio's Proler.

Proling with APD.

Installing APD.

Analyzing Trace Data.

Proling with Xdebug.

Installing Xdebug.

Tracing Script Execution.

Using Kcachegrind.

Using APC (Advanced PHP Cache).

Using ZPS (Zend Performance Suite).

Automatic Optimization.

Compiled Code Caching.

Dynamic Content Caching.

Content Compression.

Optimizing Code.

Micro-Benchmarks.

Rewrite in.

OO Versus Procedural Code.

Summary.

15. An Introduction to Writing PHP Extensions.

Introduction.

Quickstart.

Memory Management.

Returning Values from PHP Functions.

Adding Custom INI Directives.

Thread-Safe Resource Manager Macros.

Summary.

16. PHP Shell Scripting.

Introduction.

PHP CLI Shell Scripts.

How CLI Differs From CGI.

The Shell-Scripting Environment.

Parsing Command-Line Options.

Good Practices.

Process Control.

Examples.

Summary.

A. PEAR and PECL Package Index.

Authentication.

Benchmarking.

Caching.

Configuration.

Console.

Database.

Date and Time.

Encryption.

File Formats.

File System.

Gtk Components.

HTML.

HTTP.

Images.

Internationalization.

Logging.

Mail.

Math.

Networking.

Numbers.

Payment.

PEAR.

PHP.

Processing.

Science.

Streams.

Structures.

Web Services.

XML.

B. phpDocumentor Format Reference.

Introduction.

Documentation Comments.

Tag Reference.

abstract.

access.

author.

category.

copyright.

deprecated.

example.

filesource.

final.

global.

ignore.

inheritdoc (inline).

internal, internal (inline).

licence.

link.

link (inline).

name.

package.

param.

return.

see.

since.

static

staticvar.

subpackage.

todo.

uses.

var.

version.

Tag Table.

Using the phpDocumentor Tool.

C. Zend Studio Quick Start Guide.

Version 3.5.x.

About the Zend Studio Client Quick Start Guide.

About Zend.

Client Server Configuration.

Installation and Registration.

Editing a File.

Editing a File.

Working with Projects.

Advantages of Working with Projects.

How to Create a Project.

Running the Debugger.

Internal Debugger.

Remote Debugger.

Debug URL.

Configure Studio Server for Remote Debugger and Proling.

Running the Proler.

Product Support.

Getting Support.

Main Features.

Read More Show Less

Preface

Preface

"The best security against revolution is in constant correction of abuses and the introduction of needed improvements. It is the neglect of timely repair that makes rebuilding necessary."—Richard Whately

In the Beginning

It was eight years ago, when Rasmus Lerdorf first started developing PHP/FI. He could not have imagined that his creation would eventually lead to the development of PHP as we know it today, which is being used by millions of people. The first version of "PHP/FI," called Personal Homepage Tools/Form Interpreter, was a collection of Perl scripts in 1995.1 One of the basic features was a Perl-like language for handling form submissions, but it lacked many common useful language features, such as loops.

PHP/FI 2

A rewrite came with PHP/FI 22 in 1997, but at that time the development was almost solely handled by Rasmus. After its release in November of that year, Andi Gutmans and Zeev Suraski bumped into PHP/FI while looking for a language to develop an e-commerce solution as a university project. They discovered that PHP/FI was not quite as powerful as it seemed, and its language was lacking many common features. One of the most interesting aspects included the way loops were implemented. The hand-crafted lexical scanner would go through the script and when it hit the while keyword it would remember its position in the file. At the end of the loop, the file pointer sought back to the saved position, and the whole loop was reread and re-executed.

PHP 3

Zeev and Andi decided to completely rewrite the scripting language. They then teamed up with Rasmus to release PHP 3, and along also came a new name: PHP: Hypertext Preprocessor, to emphasize that PHP was a different product and not only suitable for personal use. Zeev and Andi had also designed and implemented a new extension API. This new API made it possible to easily support additional extensions for performing tasks such as accessing databases, spell checkers and other technologies, which attracted many developers who were not part of the "core" group to join and contribute to the PHP project. At the time of PHP 3's release3 in June 1998, the estimated PHP installed base consisted of about 50,000 domains. PHP 3 sparked the beginning of PHP's real breakthrough, and was the first version to have an installed base of more than one million domains.

PHP 4

In late 1998, Zeev and Andi looked back at their work in PHP 3 and felt they could have written the scripting language even better, so they started yet another rewrite. While PHP 3 still continuously parsed the scripts while executing them, PHP 4 came with a new paradigm of "compile first, execute later." The compilation step does not compile PHP scripts into machine code; it instead compiles them into byte code, which is then executed by the Zend Engine (Zend stands for Zeev & Andi), the new heart of PHP 4. Because of this new way of executing scripts, the performance of PHP 4 was much better than that of PHP 3, with only a small amount of backward compatibility breakage4. Among other improvements was an improved extension API for better run-time performance, a web server abstraction layer allowing PHP 4 to run on most popular web servers, and lots more. PHP 4 was officially released on May 22, 2002, and today its installed base has surpassed 15 million domains.

In PHP 3, the minor version number (the middle digit) was never used, and all versions were numbered as 3.0.x. This changed in PHP 4, and the minor version number was used to denote important changes in the language. The first important change came in PHP 4.1.0,5 which introduced superglobals such as and . Superglobals can be accessed from within functions without having to use the keyword. This feature was added in order to allow the register_globals INI option to be turned off. register_globals is a feature in PHP which automatically converts input variables like "?foo=bar" in http://php.net/?foo=bar to a PHP variable called $foo. Because many people do not check input variables properly, many applications had security holes, which made it quite easy to circumvent security and authentication code.

With the new superglobals in place, on April 22, 2002, PHP 4.2.0 was released with the register_globals turned off by default. PHP 4.3.0, the last significant PHP 4 version, was released on December 27, 2002. This version introduced the Command Line Interface (CLI), a revamped file and network I/O layer (called streams), and a bundled GD library. Although most of those additions have no real effect on end users, the major version was bumped due to the major changes in PHP's core.

PHP 5

Soon after, the demand for more common object-oriented features increased immensely, and Andi came up with the idea of rewriting the objected-oriented part of the Zend Engine. Zeev and Andi wrote the "Zend Engine II: Feature Overview and Design" document6 and jumpstarted heated discussions about PHP's future. Although the basic language has stayed the same, many features were added, dropped, and changed by the time PHP 5 matured. For example, namespaces and multiple inheritance, which were mentioned in the original document, never made it into PHP 5. Multiple inheritance was dropped in favor of interfaces, and namespaces were dropped completely. You can find a full list of new features in Chapter, "What Is New in PHP 5?"

PHP 5 is expected to maintain and even increase PHP's leadership in the web development market. Not only does it revolutionizes PHP's object-oriented support but it also contains many new features which make it the ultimate web development platform. The rewritten XML functionality in PHP 5 puts it on par with other web technologies in some areas and overtakes them in others, especially due to the new SimpleXML extension which makes it ridiculously easy to manipulate XML documents. In addition, the new SOAP, MySQLi, and variety of other extensions are significant milestones in PHP's support for additional technologies.

Audience

This book is an introduction to the advanced features new to PHP 5. It is written for PHP programmers who are making the move to PHP 5. Although Chapter 2, "PHP 5 Basic Language," contains an introduction to PHP 5 syntax, it is meant as a refresher for PHP programmers and not as a tutorial for new programmers. However, web developers with experience programming other high-level languages may indeed find that this tutorial is all they need in order to begin working effectively with PHP 5.

Chapter Overview

Chapter 1, "What Is New in PHP 5?" discusses the new features in PHP 5. Most of these new features deal with new object-oriented features, including small examples for each feature. It also gives an overview of the new extensions in PHP 5. Most of the topics mentioned in this chapter are explained in more detail in later chapters.

Chapter 2, "PHP 5 Basic Language," introduces the PHP syntax to those readers not familiar with PHP. All basic language constructs and variable types are explained along with simple examples to give the reader the necessary building blocks to build real scripts.

Chapter 3, "PHP 5 OO Language," continues exploring PHP 5's syntax, focusing on its object-oriented functionality. This chapter covers basics, such as properties and methods, and progresses to more complicated subjects, such as polymorphism, interfaces, exceptions, and lots more.

Using the previous chapter as a foundation, Chapter 4, "PHP 5 Advanced OOP and Design Patterns," covers some of the most advanced features of PHP 5's object model. After learning these features, including four commonly used design patterns and PHP's reflection capabilities, you will soon become an OO wizard.

Now that you are familiar with the syntax and language features of PHP, Chapter 5, "How to Write a Web Application with PHP," introduces you to the world of writing web applications. The authors show you basics, such as handling input through form variables and safety techniques, but this chapter also includes more advanced topics, such as handling sessions with cookies and PHP's session extension. You also find a few tips on laying out your source code for your web applications.

Chapter 6, "Databases with PHP 5," introduces using MySQL, SQLite, and Oracle from PHP, but focuses primarily on the PHP 5-specific details of database access. For each database, you learn about some of its strong and weak points, as well as the types of applications at which each excels. And of course, you learn how to interface with them using PHP's native functions or using PEAR DB.

All scripts can throw errors, but of course you do not want them to show up on your web site once your application has passed its development state. Chapter 7, "Error Handling," deals with different types of errors that exist, how to handle those errors with PHP, and how to handle errors with PEAR.

As one of the important new features in PHP 5 is its renewed XML support, a chapter on XML features in PHP 5 could not be missed. Chapter 8, "XML with PHP 5," talks about the different strategies of parsing XML and converting XML to other formats with XSLT. XML-RPC and SOAP are introduced to show you how to implement web services with both techniques.

Although not specifically for PHP 5, the five mainstream extensions that Chapter 9,"Mainstream Extensions," covers are important enough to deserve a place in this book. The first section, "Files and Streams," explains about handling files and network streams. A stream is nothing more than a way to access external data, such as a file, remote URL, or compressed file. The second section, "Regular Expressions," explains the syntax of a regular expression engine (PCRE) that PHP uses with numerous examples to show you how these expressions can make your life easier. In "Date Handling," we explain the different functions used to parse and format date and time strings. In "Graphics Manipulation with GD," we show you through two real-life scenarios the basic functions of creating and manipulating graphics with PHP. The last section in this chapter, "Multibyte Strings and Character Sets," explains the different character sets and the functions to convert and handle different ones, including multi-byte strings used in Asian languages.

Chapter 10, "Using PEAR," introduces PEAR, the PHP Extension and Application Repository. Starting with concepts and installation, the chapter shows how to use PEAR and maintain the local installed packages. This chapter also includes a tour of the PEAR web site.

Chapter 11, "Important PEAR Packages," gives an overview of the most important PEAR packages, along with examples. Packages covered include Template Systems, the Auth package to do authentication, form handling with the HTML_QuickForm package, and a package used to simplify caching.

Chapter 12, "Building PEAR Components," explains how to create your own PEAR package. The PEAR Coding Standard and package.xml package definition format, together with tips on including files and package layout, get you on your way to completing your first PEAR package.

Chapter 13, "Making the Move," deals with the few backward-incompatible changes that were introduced between PHP 4 and PHP 5. This chapter tells you which things you need to take care of when making your application work on PHP 5, and provides workarounds wherever possible.

Chapter 14, "Performance," shows you how to make your scripts perform better. The chapter offers tips on standard PHP usage, the use of external utilities (APD and Xdebug) to find problems in your scripts, and PHP accelerators like APC and Zend Performance Suite.

Chapter 15, "An Introduction to Writing PHP Extensions," explains how to write your own custom PHP extension. We use a simple example to explain the most important things like parameter parsing and resource management.

Chapter 16, "PHP Shell Scripting," shows you how to write shell scripts in PHP, because PHP is useful for more than just web applications. We carefully explain the differences between the CLI and CGI executables in which PHP comes, including command-line parameter parsing and process control.

This book also includes three appendices. Appendix A, "PEAR and PECL Package Index," provides an overview of all important packages, with descriptions and dependencies on other packages. Appendix B, "phpDocument Format Reference," explains the syntax as understood by the PHP Documenter tool to generate API documentation from source code. Appendix C, "Zend Studio Quick Start," is an introduction to working in the Zend Studio IDE.

A Note About Coding Styles

There are almost as many coding styles as there are programmers. The PHP examples in this book follow the PEAR coding standard, with the opening curly bracket on the line below the function name. In some cases, we've placed the curly bracket on the same line as the function name. We encourage you to adopt the style you are most comfortable with.


Note - A code continuation character, , appears at the beginning of code lines that have wrapped down from the line above it.

About the Software

Included in the back of this book is a special link to Zend.com, where you can download a fully functional, 90-day trial version of the Zend Studio IDE. Be sure to use the license key printed on the inside back cover of this book when you install Zend Studio.

The Zend Development Environment (ZDE) is a convenient tool that integrates an editor, debugger, and project manager to help you develop, manage, and debug your code. It can connect to your own installed server or directly to the Zend Studio server component. It is a powerful tool that allows you to debug your code in its natural environment.

Updates and Errata and Downloads

Updates, errata, and copies of the sample programs used in this book can be found at the following URL: http//php5powerprogramming.com. We encourage you to visit this site.

Acknowledgements

This book could not have been written without feedback from our technical reviewers; therefore, we would like to thank Marcus B_ger, Steph Fox, Martin Jansen, and Rob Richards for their excellent comments and feedback. Besides these four reviewers, there are a few more people who helped answer several questions during the writing of this book, more specifically Christian Stocker for helping with the XML chapter, Wez Furlong and Sara Golemon for answering questions about the streams layer, Pierre-Alain Joye for providing some insights in the inner workings of the GD library, and less specifically the PEAR community for their support and dedication to a great repository of usable PEAR components. Some sections in this book were contributed by co-authors; Georg Richter contributed the MySQLi section of the database chapter, and Zeev Suraski added the section on Zend's Performance Suite.

We would also like to thank Mark L. Taub and the editorial team of Pearson PTR for the things they are good at doing: organizing, planning, and marketing this book, and making sure everything fits together. Thanks to Janet Valade, for helpful developmental editing support, and our project editor Kristy Hart, who helped us wrap up the book under pressure and put the final touches on it.

Enjoy!

Andi, Stig, and Derick


1 http://groups.google.com/groups?selm=3r7pgp$aa1@ionews.io.org.
2 http://groups.google.com/groups?selm=Dn1JM9.61t%40gpu.utcc.utoronto.ca.
3 http://groups.google.com/groups?selm=Pine.WNT.3.96.980606130654.-317675I-100000%40shell.lerdorf.on.ca.
4 http://www.php.net/manual/en/migration4.php.
5 http://www.php.net/release_4_1_0.php.
6 http://zend.com/engine2/ZendEngine-2.0.pdf.

© Copyright Pearson Education. All rights reserved.

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing 1 Customer Reviews
  • Anonymous

    Posted January 17, 2005

    PHP 5 In-Depth for the Already Initiated

    OK, so you've created your first web page with PHP and MySQL and it's all right--it does what you want--but you want something more. Something in there is lacking. You've been searching for the best way to get your object-oriented applications working properly, or you need to find a better way to assimilate your data graphically for your boss and the only computer technology he understands is the World Wide Web. Welcome to the world of power programming with the latest version of PHP. <p> One of the latest books in the Prentice Hall's Bruce Perens' Open Source Series is this one called 'PHP 5 Power Programming.' Written by Andi Gutmans, the lead PHP 5 designer, Stig Saether Bakken, the creator of PEAR, and Derick Rethans, a key PHP contributor, this book is a self-proclaimed tome of wonders which contains insights into all the key features of the latest version of PHP and the keys to unlocking all its wholesome goodness. <p> After spending a little time showing the evolution of PHP, the authors dive right in. They only devote a brief introduction to the basics of PHP. Clearly, if all you've done is convert a six-line BASH script into PHP, you might check out a less involved reference first. You're expected to be tired with the PHP basics already when you dive into this work, because it's barely a chapter gone before you start getting involved in advanced constructs like iterators, object polymorphism, overloading and exception handling. The best part is the authors never leave you floundering about a simple topic just because they're in the midst of a complicated one. They never let you forget how to unset a cookie, while explaining all of the safeguards necessary to maintain a safe operating environment for a published Web application. <p> Some of the topics that can be found in this very involved book are: stucturing and designing Web applications, integration with MySQL for data storage and recovery, working with the GD libraries for extensive graphical manipulation, and a detailed discussion and reference on PEAR, the PHP Extension and Application Repository, which does for PHP what CPAN does for Perl. There are also sections with great insite into using PHP with XML and SOAP, regular expressions and streams. And there's even a detailed chapter towards the back about benchmarking your PHP performance and enhancing its operation. Three appendices provide information on the packages available through PEAR and PECL, the phpDocumentor application and Zend Studio. <p> For a technical manual, this book is surprisingly well laid out. You can use the index in the back to find the topic of your interest, but just skimming through reveals that everything is put where you can find it. Reading the book cover to cover works well also. I doubt the average reader will skip over much as this reference is chock full of good information and written with a style that keeps the reader interested, even if it's a topic he might already be familiar with. <p> For anyone who already has the fundamentals of PHP in pocket, but wants to get into the nuts and bolts of the new version and explore all of the depth and richness of some of its advanced features, this book is definitely a keeper. At 689 pages, it's long enough to contain a treasure trove of information, but short enough not to turn anyone away by its sheer volume. The best part is, all of this information is straight from the guys who know it best, so you can be sure that all of the best tips are included. And by the end of the book it's clear that PHP has evolved a great deal more robustness than it's original title and function of hypertext preprocessor had likely ever imagined.

    Was this review helpful? Yes  No   Report this review
Sort by: Showing 1 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)