More Books
PHP 5 Unleashed
PHP 5 Unleashed
Table of Contents
Copyright
Lead Author
Contributing Authors
Acknowledgments
We Want to Hear from You!
Reader Services
Introduction
Organization of the Book
Part I. Working with PHP for General Web Development
Chapter 1. Basic PHP Development
How PHP Scripts Work
Basic PHP Syntax
Basic PHP Data Types
Variable Manipulation
Control Structures
User-Defined Functions
Dynamic Variables and Functions
Multiple File PHP Scripts
References
Strings in PHP
Comparing Strings
Advanced String Comparison
Search and Replacement
Formatting Strings
Strings and Locales
Formatting Date and Time Values
Summary
Chapter 2. Arrays
Basic Arrays
Implementing Arrays
More Array Materials
Chapter 3. Regular Expressions
The Basics of Regular Expressions
Limitations of the Basic Syntax
POSIX Regular Expressions
Perl-Compatible Regular Expressions (PCRE)
PCRE Modifiers
A Few Final Words
Chapter 4. Working with Forms in PHP
HTML Forms 101
Working with Form Submissions in PHP
Summary
Chapter 5. Advanced Form Techniques
Data Manipulation and Conversion
Form Data Integrity
Form Processing
Summary
Chapter 6. Persistent Data Using Sessions and Cookies
HTTP Cookies
PHP Sessions
Advanced Sessions
Summary
Chapter 7. Using Templates
The What and Why of Templates
The Smarty Template Engine
Summary
Part II. Advanced Web Development
Chapter 8. PEAR
What Is PEAR?
Getting and Installing PEAR
Using the PEAR Package Manager
Using the PEAR Website
Using PEAR Packages in Applications
Summary
Reference
Chapter 9. XSLT and Other XML Concerns
Relating XML to HTML
Using XSLT to Describe HTML Output Using XML Input
PHP4 and XSLT Using the DOM XML Module
PHP4 and XSLT Using the XSLT Module
PHP5 and XSLT
Accessing XML Data Using SimpleXML
Generating XML Documents Using PHP
Summary
References
Chapter 10. Debugging and Optimizations
Debugging Your PHP Scripts
Optimizing Your PHP Scripts
Summary
Chapter 11. User Authentication
Authenticating Users in PHP
Securing PHP Code
Summary
Chapter 12. Data Encryption
Shared Secret Versus Public Key
Shared Secret Algorithms
Public Key Cryptography
Using Public Keys in PHP
Summary
Chapter 13. Object-Oriented Programming in PHP
Why Objects?
Creating Basic Classes
Advanced Classes
Special Methods
Class Autoloading
Object Serialization
Exceptions
Iterators
Summary
Chapter 14. Error Handling
The PHP Error-Handling Model
What to Do About Errors
The Default Error Handler
Error Suppression
Custom Error Handlers
Causing Errors
Putting It All Together
Summary
Chapter 15. Working with HTML/XHTML Using Tidy
Introduction
Basic Tidy Usage
Tidy Configuration Options
Using the Tidy Parser
Applications of Tidy
Summary
Chapter 16. Writing Email in PHP
The MIME Protocol
Implementing MIME Email in PHP
Summary
Part III. Building Applications in PHP
Chapter 17. Using PHP for Console Scripting
Core CLI Differences
Working with PHP CLI
CLI Tools and Extensions
Summary
Chapter 18. SOAP and PHP
What Are Web Services?
Installation
Creating Web Services
Consuming Web Services
Looking for Web Services
Summary
Chapter 19. Building WAP-Enabled Websites
What Is WAP?
System Requirements
Introduction to WML
Serving WAP Content
Sample Applications
Summary
Part IV. I/O, System Calls, and PHP
Chapter 20. Working with the File System
Working with Files in PHP
File Permissions
File Access Support Functions
Summary
Chapter 21. Network I/O
DNS/Reverse DNS Lookups
Socket Programming
Network Helper Functions
Summary
Chapter 22. Accessing the Underlying OS from PHP
Introduction
Unix-Specific OS Functionality
Platform-Independent System Functions
A Brief Note About Security
Summary
Part V. Working with Data in PHP
Chapter 23. Introduction to Databases
Using the MySQL Client
Basic MySQL Usage
Summary
Chapter 24. Using MySQL with PHP
Performing Queries from PHP
A MySQLi Session Handler
What Is a Custom Session Handler?
Summary
Chapter 25. Using SQLite with PHP
What Makes SQLite Unique?
Basic SQLite Functionality
Working with PHP UDFs in SQLite
Odds and Ends
Summary
Chapter 26. PHP's dba Functions
Preparations and Settings
Creating a File-Based Database
Writing Data
Reading Data
Sample Application
Conclusion
Part VI. Graphical Output with PHP
Chapter 27. Working with Images
Basic Image Creation Using GD
Using the PHP/GD Drawing Functions
Working with Colors and Brushes
Using Fonts and Printing Strings
General Image Manipulation
Other Graphics Functions
Summary
Chapter 28. Printable Document Generation
A Note Regarding the Examples in This Chapter
Generating Dynamic RTF Documents
Generating Dynamic PDF Documents
Related Resources
Part VII. Appendixes
Appendix A. Installing PHP5 and MySQL
Installing PHP5
Installing MySQL and PHP Modules
Installing PEAR
Appendix B. HTTP Reference
What Is HTTP?
PHP Programming Libraries for HTTP Work
Understanding an HTTP Transaction
HTTP Client Methods
What Comes Back: Server Response Codes
HTTP Headers
Encoding
Identifying Clients and Servers
The "Referer"
Fetching Content from an HTTP Source
Media Types
Cookies: Preserving State and a Tasty Treat
Security and Authorization
Client-Side Caching of HTTP Content
Appendix C. Migrating Applications from PHP4 to PHP5
Configuration
Object-Oriented Programming (OOP)
New Behavior of Functions
Further Reading
Appendix D. Good Programming Techniques and Performance Issues
Common Style Mistakes
Common Security Concerns
Style and SecurityLogging
Summary
Appendix E. Resources and Mailing Lists
Relevant Websites
Mailing Lists and Newsgroups
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

Special Methods

In this section, I will introduce a number of special methods that you can use within your classes. Although you have already been introduced to some of the special-use methods such as __construct(), __destruct(), and __clone(), a number of methods provide a great deal of functionality if used properly. To begin, let's look at the idea of getter and setter methods.

Getters and Setters

Getter and setter methods are methods that are used to provide a catch-all interface when accessing properties in objects. These methods, named __get() and __set() respectively, will be called when a particular property was not defined. The prototype for these special methods are as follows:

function __get($name);
function __set($name, $value);

In both instances, $name represents the name of the variable that the script attempted to access but that did not exist. As you might expect, the $value of the __set() method represents the new value attempting to be set to the nonexistent value.

NOTE

Getter and setter methods are called only when the desired property does not exist in any way within the object. If the property initially does not exist but is then added to the instance via the set method (as shown below):

function __set($name, $value) {
     $this->$name = $value;
}

neither the __get() or __set() methods will be called in the future.


Getter and setter methods are useful in situations, such as in dealing with Web services or container objects where the properties available within an instance of a class may not be completely known until the execution of the script.

The __call() Method

Like getters and setters, which enable you to dynamically handle property access within your PHP scripts, the __call() method is used to provide a catch-all for method calls within an object. When an attempt to call a method not previously defined within the class is received, if possible the __call method will be called. The prototype of this method is as follows:

function __call($method, $arguments);

$method is a string representing the name of the method that was called, and $arguments is an indexed array containing the parameters passed to the called method. Like getters and setters, the __call() method is useful when a complete function list is not available until the execution of the script. Alternatively, another application of the __call() method is to provide a catch-all method to handle attempts to call invalid methods in your PHP scripts, as shown in Listing 13.21:

Listing 13.21. Using the __call() Method
<?php
     class ParentClass {
          function __call($method, $params) {
               echo "The method $method doesn't exist!\n";
          }
     }
     class ChildClass extends ParentClass {
          function myFunction() {
               /* Function Logic */
          }
     }
     $inst = new ChildClass();
     $inst->nonExistentFunction();
?>

When Listing 13.21 is executed, an attempt will be made to call a method that is not defined. However, rather than causing a fatal error, the invalid call will trigger a call to the __call() method and allow you to handle the error cleanly.

The __toString() Method

The last special method I will be discussing in the new PHP 5 object model is the __toString() method. This method is designed to provide an easy way to access the string representation of a complex object. When defined, PHP will call this method under certain circumstances when the object would be better treated as a string (such as when it is displayed using echo or print statements). The value of the string returned by the __toString() method can be anything; however, it makes sense that it must somehow be representative of the object itself. Consider Listing 13.22, which implements the __toString() method and uses its functionality:

Listing 13.22. Using the __toString() Method
<?php
    class User {
            private $username;
            function __construct($name) {
                    $this->username = $name;
            }
            public function getUserName() {
                    return $this->username;
            }
            function __toString() {
                    return $this->getUserName();
            }
    }
    $user = new User("john");
    echo $user;
?>

In Listing 13.22, we have defined a simple class User that contains shell functionality for a simple User account class. This class contains a single private property, $username, which is available by calling the getUserName() method of the class. However, because this class also implements the __toString() method (which calls getUserName() itself), any instance of the class can be treated as a string directly, as shown by the echo statement. Although in this case the __toString() method was used in a fairly simplistic fashion, in a more complex object it can be effectively used to produce a string representation in any format desired.