The php anthology 2nd edition 2007 - phần 2 pot

55 282 0
The php anthology 2nd edition 2007 - phần 2 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

32 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com HTML.class.php Here’s an example of how the HTML class could be used: echo HTML::p('This is a static method!'); This example would output as follows:

This is a static method!

Notice that when we use a static method, we use the :: operator, rather than the object -> operator, to access the method You may have noticed that this is the same operator used to call a method of the parent class of the current object, as in parent::method() The parent class usage is a special case where inheritance is concerned, as the parent class method retains access to the object’s instance data, and therefore isn’t static $this Can’t be Used in Static Methods As static methods are used without the instantiation of an object, the $this variable can’t be used in static methods Now to extend this example a bit—and possibly to excite your interest in OOP in PHP into the bargain—imagine for a moment that we’ve added a static method and a corresponding class for each possible HTML element to our HTML class Re­ membering that one HTMLElement object can be passed to the constructor of another HTMLElement object as its content, we can now create methods for all HTML elements that we can use as demonstrated in the following example: Introduction Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com echo HTML::div(HTML::h1('Welcome to my web site!'), array('id' => 'header')); This example would output the following HTML: Welcome to my web site! Taking the above example as your goal, I’ll leave the implementation of such an API up to you Come on—with this introduction to OOP under your belt, it should be easy! How I write portable PHP code? Not all PHP installations are the same Depending on version and configuration settings in your php.ini file, your script may or may not run correctly on another server on which PHP is installed However, you should consider adopting a number of generally accepted best practices to make life easier and minimize the need to rewrite code for other servers Solution The list of generally accepted best practices include, keeping your configuration central, writing your code to be reusable, always using the full PHP tags, always using supergobal variables and never using register_globals and always checking for magic quotes Keeping Configuration Central For most PHP applications, it will be necessary to write configuration information describing the environment in which the script will run, including database usernames and passwords, directory locations, and so on As a general rule, try to keep the majority of this information in a single place—maybe even a single file—so that when you need to modify the information, you can make all the necessary changes in one place That said, when you’re building modular applications, you may want to store local elements of the configuration to a specific module within the module itself, rather than in a central location The way each of us chooses to store this information is a matter of personal choice In some cases, it may be worth considering the use of an XML file, or storing some 33 34 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com of the information in a database It’s also worth being aware of the parse_ini_file function.25 A simple but effective storage mechanism is to place all the settings into a single file as PHP constants, which makes them available from any function or class in your application Here’s an example: Constants need to be used with caution, though In order for your functions and classes to be reusable in other applications, they shouldn’t depend on constants of a fixed name; rather, they should accept configuration information as arguments—an approach that will allow for greater code reuse In such cases, it’s best to use PHP variables in your central configuration file, which you can then pass to functions and classes as required For example, when we’re connecting to database, we can identify a number of variables that we need to have stored in a central location: the server hostname, the username, and the password We can use the require_once function to create a file called, for instance, config.php, and place it outside the public web directories This approach helps to ensure that users don’t accidentally browse to the file con­ taining this critical information—a situation that would place the site’s security at risk Recycling and Reuse It’s easy to say, but if you find yourself writing any more than one PHP script in your life, you need to start thinking about ways to make your code reusable before you suffer premature hair loss! If you end up working on other sites or applications, you’ll appreciate having ready code that you can simply plug into your new project Also, if you’re writing code 25 http://www.php.net/manual/en/function.parse-ini-file.php Introduction Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com that other people will integrate with existing applications on their web sites, you need to package it in a form that doesn’t place requirements on the code they’re already using For example, if your application has some kind of user authentication system, you’ll want to ask yourself if it can be integrated with the systems that site owners are already using—systems with which large databases of users are likely already associated The best approach is to write object oriented code with a mind to creating reusable components, or pieces of functionality Some people argue that creating PHP applic­ ations using object oriented code results in slower-running applications and should be avoided at all costs What they forget to mention is that object oriented program­ ming delivers a drastic increase in your code’s performance After all, fast program­ mers cost more than fast microprocessors! A number of important points must be considered when you’re measuring the po­ tential of your code for reuse: ■ What happens when the project’s requirements change? ■ How easy is it to add new features to your code? ■ Are you still able to understand the code after a long period of time? ■ Can your code be integrated easily with other applications? ■ Will the assumptions you’ve made in your code apply to your work on other sites? This book will provide many hints and suggestions to help you to write reusable code, although an in-depth analysis of PHP applications design as a whole is beyond its scope As you read this book, you should be able to identify some of the critical factors as subjects for further investigation You have one main responsibility to yourself as an experienced PHP developer: to keep expanding your knowledge of the more esoteric aspects of software development, such as design patterns and enterprise application architecture, as a means to improve your development tech­ nique and, more importantly, save yourself time The broader your knowledge, the lower the risk of failure when you land the next big project Portability Essentials Here are three steps you should take to ensure the portability of your PHP code 35 36 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Using the Full Tags PHP supports a variety of tag styles to mark up sections of your code, including the short tags (), and ASP-style tags () Tag style support is controlled from php.ini with the settings short_open_tag and asp_tags Be aware, though, that while you may have these settings switched on, other server administrators may not, which can be problematic The short tag style, for example, causes issues when the PHP is mixed with XML documents that use processing instructions like this: If you have a document that contains PHP and XML, and you have the short_open_tag setting turned on, PHP will mistake the XML processing instruction If we include this code at the start of any file in which we accept data from a query string, a form post, or a cookie, we’ll remove any slashes added by magic quotes, should this functionality be switched on Summary Are you ready to jump in and try the PHP waters? This chapter has showed you how to keep your head up and tread water You may not be a professional swimmer yet, but with The PHP Manual by your side—as well as this book—we’ll keep you afloat, introduce you to some of the beauty of the PHP ocean, and eventually show you how to glide through the waters with grace! Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Chapter Using Databases with PDO In the “old days” of the Internet, most web pages were nothing more than text files containing HTML When people visited your site, your web server simply made the file available to their browsers This approach started out fine, but as web sites grew, and issues such as design and navigation became more important, developers found that maintaining consistency across hundreds of HTML files was becoming a massive headache To solve this problem, it became popular to separate variable content (articles, news items, and so on) from the static elements of the site—its design and layout If a database is used as a repository to store variable content, a server-side language such as PHP performs the task of fetching that data and placing it within a uniform layout template This means that modifying the look and feel of a site can be handled as a separate task from the maintenance of content And maintaining consistency across all the pages in a web site no longer consumes a developer’s every waking hour PHP supports all the relational databases worth mentioning, including those that are commonly used in large companies: Oracle, IBM’s DB2, and Microsoft’s SQL Server, to name a few The three most noteworthy open source alternatives are 40 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com SQLite, PostgreSQL, and MySQL PostgreSQL is arguably the best database of the three, in that it supports more of the features that are common to relational databases SQLite is the perfect choice for smaller applications that still require database cap­ ability MySQL is a popular choice among web hosts that provide support for PHP, and for this reason is typically easier to find than PostgreSQL This chapter covers all the common operations that PHP developers perform when working with databases: retrieving and modifying data, and searching and backing up the database To achieve these tasks, we’ll use the built-in PDO extension, rather than database-specific extensions The examples we’ll work with will use a single table, so no discussion is made of table relationships here For a full discussion of that topic, see Kevin Yank’s Build Your Own Database Driven Website Using PHP & MySQL, 3rd Edition (SitePoint, Melbourne, 2006)1 The examples included here work with the MySQL sample database called “world,” though all the interactions we’ll work through can be undertaken with any database supported by PDO The SQL file for the world database is available at http://dev.mysql.com/doc/#sampledb and the instructions explaining its use can be found at http://dev.mysql.com/doc/world-setup/en/world-setup.html What is PDO? PDO, the PHP Data Objects extension, is a data-access abstraction layer But what the heck is that? Basically, it’s a consistent interface for multiple databases No longer will you have to use the mysql_* functions, the sqlite_* functions, or the pg_* functions, or write wrappers for them to work with your database Instead, you can simply use the PDO interface to work with all three functions using the same methods And, if you change databases, you’ll only have to change the DSN (or Data Source Name) of the PDO to make your code work.2 PDO uses specific database drivers to interact with various databases, so you can’t use PDO by itself You’ll need to enable the drivers you’ll use with PDO, so be sure http://www.sitepoint.com/books/phpmysql1/ That’s all you’ll have to so long as you write your SQL in a way that’s not database specific If you try to stick to the ANSI 92 standard [http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt], you should generally be okay—most databases support that syntax Using Databases with PDO Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com to research how to it for your specific host operating system on the PDO manual page.3 PDO is shipped with PHP 5.1 and is available from PECL for PHP 5.0 Unfortunately, as PDO requires the new PHP object oriented features, it’s not available for PHP In this book, all of our interactions with the database will use PDO to interact with the MySQL back end How I access a database? Before we can anything with a database, we need to talk to it And to talk to it, we must make a database connection Logical, isn’t it? Solution Here’s how we connect to a MySQL database on the localhost: mysqlConnect.php (excerpt) We’d use this code to connect to a SQLite database on the localhost: http://www.php.net/pdo/ 41 72 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Catering to Platform Differences You may have noticed that the above MySQLDump class will only work on a *nix server What if your database server uses a Windows box? I offer the following solution to circumvent this problem First we define an abstract MySQLDump class, then we extend it to create a class for each platform, and finally we create a factory method to instantiate the correct MySQLDump object needed Here’s our abstract MySQLDump class: AbstractMySQLDump.class.php (excerpt) require_once 'MySQLDump_ms.class.php'; require_once 'MySQLDump_nix.class.php'; abstract class MySQLDump { public static function factory($dbUser, $dbPass, $dbName, $dest, $zip) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { return new MySQLDump_ms($dbUser, $dbPass, $dbName, $dest, $zip); } else { return new MySQLDump_nix($dbUser, $dbPass, $dbName, $dest, $zip); } } abstract public function construct($dbUser, $dbPass, $dbName, $dest, $zip = 'gz'); public function backup() { system($this->cmd, $error); if ($error) { throw new MySQLDumpException( 'Backup failed: Command = ' $this->cmd ' Error = ' $error); } } Using Databases with PDO Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com } class MySQLDumpException extends Exception {} The backup method represents our backup API Child classes need to implement a custom constructor that sets the cmd property Overriding the backup method is optional The static method factory will instantiate a MySQLDump object instance based on the PHP_OS constant—representing the host platform We’ve also added a custom exception class, MySQLDumpException, for error handling The *nix version of our backup class will contain an implementation similar to the solution class above, but we’ll need to change the class definition so that it extends the abstract MySQLDump class: MySQLDump_nix.class.php (excerpt) require_once 'AbstractMySQLDump.class.php'; class MySQLDump_nix extends MySQLDump { protected $cmd; public function construct($dbUser, $dbPass, $dbName, $dest, $zip = 'gz') { $zip_util = array('gz'=>'gzip','bz2'=>'bzip2'); if (array_key_exists($zip, $zip_util)) { $fname = $dbName '.' date("w") '.sql.' $zip; $this->cmd = 'mysqldump -u' $dbUser ' -p' $dbPass ' ' $dbName '| ' $zip_util[$zip] ' >' $dest '/' $fname; } else { $fname = $dbName '.' date("w") '.sql'; $this->cmd = 'mysqldump -u' $dbUser ' -p' $dbPass ' ' $dbName ' >' $dest '/' $fname; } } } 73 74 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com We can then make an implementation for the Windows platform: MySQLDump_ms.class.php (excerpt) require_once 'AbstractMySQLDump.class.php'; class MySQLDump_ms extends MySQLDump { protected $cmd; public function construct($dbUser, $dbPass, $dbName, $dest, $zip = 'none') { $fname = $dbName '.' date("w") '.sql'; $this->cmd = 'mysqldump -u' $dbUser ' -p' $dbPass ' ' $dbName ' >' $dest '\\' $fname; } } The Windows version above includes changes to suit the Windows path and ignores the $zip argument due to the lack of gzip and bzip2 on that platform This class also assumes that the path to the mysqldump.exe executable file is in the system PATH environment variable Here’s an example of a backup script that makes use of the above classes on a Windows box: backup2.php (excerpt) Since we’ve used an abstract class to define our API, the use of the class remains the same no matter what platform it’s used on, as long as it’s one of our supported platforms Summary There you have it—our whirlwind tour of PDO and databases is done! By now, you should have a grasp of the basic workings between PHP’s PDO extension and data­ bases We also covered the topics of searching, stored procedures, protecting your script from SQL injection attacks, writing flexible code, and making database backups Being able to work comfortably with a database is part of a strong foundation for PHP, and learning to make the most of PHP’s PDO extension only makes it easier Use the examples and solutions presented here to help build on your existing database skills I also hope you’ll take the time to learn more about SQL and your database Learning the nuances and capabilities of your chosen database platform can only help make your code more efficient and elegant over time 75 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Chapter Strings Strings are arguably the basis, the raison d’etre, nay, the beating heart of PHP After all, PHP really boils down to the input and output of strings So, it’s hardly surprising that PHP has more string-related functions than almost any other scripting language! Unlike other languages such as C, strings in PHP are not arrays of characters; they’re considered to be a simple type, or scalar In PHP, strings can be defined using either single (') or double (") quotes Strings defined using double quotes are interpol­ ated—this means that variables within the string are substituted for their values Use single quotes for strings that require no interpolation Strings themselves are case sensitive, but a number of string functions allow operations on strings in a case-insensitive manner The PHP manual web site’s String Functions page has links to all the string-related functions available.1 PHP’s variable interpolation is one of the many features that make the language so quick and easy to use However, there are limits to its capabilities First, have a look at this example: http://www.php.net/strings/ 78 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Here, we have a very simple variable, $who, that has a value of 'world' When we place the variable name in the string, we end up with an output of “Hello world” While this example is very simple, you may run into situations where your data is contained in an array or a complex object and in these cases, we need to help the PHP interpreter along We either enclose the variable in braces ({ and }) or use concatenation—the operator Here’s an example of what I mean: In the above example we demonstrate variable interpolation by wrapping our vari­ ables in braces The final statement shows that we can achieve the same output if we use simple concatenation Even though strings are considered to be scalar values, PHP has the ability to treat strings as arrays in certain situations Consider this quick example where we output a string letter by letter in a for loop: Strings Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Notice that we output a single letter from the string using array notation That code will output the following: Hello Worl ➥d! This ability to treat strings as arrays only goes so far, though You can’t, for example, pass a string to one of PHP’s array functions How I output strings safely? The most common activity you will perform with strings is to output them Whether you’re outputting strings to a browser or to a database, you’ll need to be careful to encode the strings properly Some string data has special meaning and may, to take a best-case scenario, obscure the output; in the worst case, outputting the wrong string data can cause security vulnerabilities Solution When outputting a string to a browser, we must consider several aspects: ■ Are you outputting a URL inside an tag? ■ Are you outputting to an HTML form element? ■ Do you want to show, or remove any HTML? ■ Do you need to preserve formatting? Let’s look at an example: Here, the $text variable string contains an ampersand (&) which we need to escape We need to perform two separate actions on the text in order to escape the ampersand in the two places where it is used Firstly, we need to use rawurlencode function to convert the ampersand and spaces to a valid URL string The second operation 79 80 The PHP Anthology Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com uses the htmlentities to turn the ampersand into a valid HTML entity because “&” is a special character in HTML (and XML) The resulting HTML looks like this: ➥Buy Ben & Jerrys Ice Cream In the URL string, the ampersand has been replaced with %26, and spaces by %20, and the ampersand in the link text has been replaced by & More considerations arise when you’re outputting strings as a means to prepopulate form fields—perhaps you want to display default data, display user input for con­ firmation purposes, or deal with an error Again, the htmlentities function gets the job done:

Ngày đăng: 13/08/2014, 09:20

Mục lục

  • The PHP Anthology

    • Table of Contents

    • Preface

      • Who Should Read this Book?

      • What’s Covered in this Book?

      • Running the Code Examples

      • The Book’s Web Site

        • The Code Archive

        • Updates and Errata

        • The SitePoint Forums

        • The SitePoint Newsletters

        • Your Feedback

        • Conventions Used in this Book

          • Code Samples

          • Tips, Notes, and Warnings

          • Introduction

            • Where do I get help?

              • Solution

                • RTFM: Read the Fine Manual

                  • I. Getting Started and II. Installation and Configuration

                  • III. Language Reference

                  • IV. Security

                  • V. Features

                  • VI. Function Reference

                    • PHP Extensions

                    • User Comments

                    • Other Resources

                    • What is OOP?

                      • Solution

                        • Classes Explained

                          • Encapsulation and Visibility

                          • Constructors and Destructors

Tài liệu cùng người dùng

Tài liệu liên quan