Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×

Extending and Embedding PHP 128

Sebastian Bergmann writes "PHP is a widely-used general-purpose scripting language that is especially suited for Web development. The interpreter that executes programs written in the PHP programming language has been designed from the ground up to be easily embeddable (for instance into the Apache Web Server) and extendable. This extensibility is one of the reasons why PHP became the favourite "glue" of the Web: functionality from existing third-party libraries (database clients or image manipulation toolkits, for instance) can be made available through PHP with the ease of use you expect from a scripting language." Read the rest of Sebastian's review.
Extending and Embedding PHP
author Sara Golemon
pages 448
publisher SAMS
rating 9
reviewer Sebastian Bergmann
ISBN 067232704X
summary The new standard work on extending and embedding PHP.


"Extending and Embedding PHP" by Sara Golemon, a long-time contributor to the PHP project, remedies the fact that the internals of PHP are far from being as well documented as the rest of PHP. It brings writing extensions for PHP "to the masses", so to speak.

After a short introduction that makes the reader familiar with terms like PHP Extension, Zend Extension, Userland, and Internals that are used throughout the book, Chapter 1 ("The PHP Life Cycle") opens with an overview of how the PHP Interpreter works and what parts (TSRM, Zend Engine, SAPI, "PHP") it comprises.

Chapter 2 ("Variables from the Inside Out") shows how PHP handles and stores variables internally. The reader learns how to distinguish types, set and retrieve values, as well as how to work with symbol tables. It is in this chapter that the fundamental unit of data storage in PHP, the so-called zval (short for Zend Value) is discussed.

Chapter 3 ("Memory Management") builds upon the previous chapter and discusses more advanced operations on zvals, for instance creating and dealing with copies of a zval or the destruction of a zval when it is no longer needed. To this extent, the Zend Memory Manager is discussed as well as underlying principles such as Reference Counting and Copy-on-Write, for instance.

Chapter 4 ("Setting Up a Build Environment") guides the reader through setting up an environment, either on *NIX or on Microsoft Windows, for the development and debugging of PHP and PHP extensions.

After these first four chapters, the reader is ready to go about writing his or her first PHP extension. Chapter 5 ("Your First Extension") takes the reader through the steps necessary to write and build a simple working PHP extension. The following chapters build upon the knowledge gained here, so that the reader can ultimately implement or change any type of PHP feature.

Chapter 6 ("Returning Values") explains how to pass values (by value, by reference, and through their parameter stack using references) from internal (C-level) functions or methods to userland (PHP-level).

Chapter 7 ("Accepting Parameters") deals with the mechanisms involved in accepting parameters from userland calls to an internal function or method. This includes the discussion of the zend_parse_parameters() API which makes the parameters that are passed to the internal function or method as indirect zval references usable in your C-code. The handling of optional and arbitrary numbers of parameters is explained as well as the usage of type hinting and its arg_info API.

Chapter 8 ("Working with Arrays and Hash Tables") explains the Zend Engine's HashTable API, which is used to store any piece of data of any size, in detail. Its different data storage mechanisms supported are introduced and compared. To quote from the book, "A HashTable is a specialized form of a doubly linked list that adds the speed and efficiency of vectors in the form of lookup indices". Since these structures are used heavily throughout the Zend Engine and PHP and its extensions, a good understanding of this API is vital for any aspiring PHP extension developer.

Chapter 9 ("The Resource Data Type") introduces the reader to the first complex data type (excluding the Array data type that was discussed in the previous chapter, which is just a collection containing primitive data types like strings or numbers). A resource can be, for instance, a connection to a database. It allows the PHP extension developer to "connect abstract concepts like opaque pointers from third-party libraries to the easy-to-use userspace scripting language that makes PHP so powerful".

Chapters 10 ("PHP 4 Objects") and Chapter 11 ("PHP 5 Objects") delve into the last data type supported by the Zend Engine: objects. Sara Golemon dedicates one chapter each to the respective APIs of PHP 4 and PHP 5 because of the huge advancements that were introduced in PHP 5 and that totally changed the APIs.

After the previous chapter, all data types supported by the Zend Engine have been discussed and the book revisits a topic discussed earlier in the book: that of the PHP Interpreter's life cycle. Chapter 12 ("Startup, Shutdown, and a Few Places in Between") explains how to add state to a PHP extension by using thread-safe globals. Along the way, concepts such as internal and external (super) globals as well as thread safety are discussed.

Chapter 13 ("INI Settings") shows how a PHP extension can be made ready for runtime configuration through php.ini settings.

The next three chapters ("Accessing Streams", "Implementing Streams", and "Diverting the Stream") make the reader familiar with yet another important API of PHP: the Streams API. All file input/output in PHP userspace is processed through PHP's Streams Layer. This layer, that was introduced in PHP 4.3, is what makes working with files, compressed files, and remote files, for instance, seamlessly in PHP. The reader learns how to work with streams as well as how to expose streamable resources, whether remote network input/output or local data sources, using the Streams API, thus avoiding the need to reimplement all the tedious bits and pieces that are normally associated with this.

Chapter 17 ("Configuration and Linking") builds upon the tools and techniques introduced in Chapter 4 and adds the GNU autotools (autoconf, automake, and friends) to the reader's set of tools. These tools, if used correctly, allow the extension to be built in environments that the extension author does not know or has no access to.

Chapter 18 ("Extension Generators") takes a look at ext_skel (which comes with the source distribution of PHP) and PECL_Gen (which can be obtained, as the name suggests, from PECL, the PHP Extension Community Library). These two tools automate most of the steps described in the previous chapter and take a lot of tedious work out of the extension writer's hands.

Starting with simple embedding examples, the reader learns in Chapter 19 ("Setting Up a Host Environment") and Chapter 20 ("Advanced Embedding") how the PHP Interpreter can be embedded into almost any other application.

The book concludes with the "Zend API Reference", "PHP API Reference", "Extending and Embedding Cookbook", and "Additional Resources" appendixes. The first two are a great resource for both novice and experienced PHP extension writers (even for people working on PHP and the Zend Engine itself). The third features a collection of common use code snippets while the last one points the reader into the direction of PECL by suggesting a couple of existing extensions to look at and learn from.

Since the topic of this book is to extend the PHP Interpreter using extensions written in the C programming language (or to embed it into an application that is written in C), a good understanding of C syntax, its datatypes, and pointer management is important to get the most out of this book.

Being a contributor to the PHP project for about six years now, I have been looking forward to this book. True, there is always the source code of the PHP Interpreter as a source of information on how "things work". But although being the ultimate reference, reading the source code cannot replace a thoughtfully structured and well written guide that gets you started. If you are looking for such a guide, look no further: you will find it in this excellent book.

Although it deals with a very technical topic, "Extending and Embedding PHP" is readable and the many code examples are easy to follow. The reader profits from the knowledge of the author, who has been involved in the PHP project as a core developer for over four years now and is also the author and maintainer of a dozen PHP extensions that are available through PECL. The book covers both major versions of PHP that are currently used, PHP 4 and PHP 5, and it will continue to serve its purpose when PHP 6 comes out next year.

Sebastian Bergmann spends his free time with the development of Free Software, is a member of the PHP and Gentoo Linux development teams and author of a variety of PHP software projects such as PHPUnit."


You can purchase Extending and Embedding PHP from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This discussion has been archived. No new comments can be posted.

Extending and Embedding PHP

Comments Filter:
  • Sara Golemon (Score:2, Interesting)

    by larry bagina ( 561269 ) on Monday July 31, 2006 @04:03PM (#15819994) Journal
    I knew her when she worked at berkeley (I think she works for yahoo! now). She really knows her shit.
  • by suggsjc ( 726146 ) on Monday July 31, 2006 @04:04PM (#15819997) Homepage
    Ok, so 5 of the first 7 comments were trolling about how bad PHP is, insecure, buggy, etc (and I think they even managed to take a shot at Bush???)

    I've used PHP for some very small applications/sites. Can anyone give an unbiased (almost impossible I know) state of affairs for PHP? I know that it is a pretty common tool, has its strengths and weaknesses. However, is it really that bad or is bashing it just the current /. thing to do?
  • Re:Sara Golemon (Score:3, Interesting)

    by SIGALRM ( 784769 ) on Monday July 31, 2006 @04:22PM (#15820141) Journal
    Yes, Sara is now at Yahoo [flickr.com], at least the last I was aware of.
  • by Bogtha ( 906264 ) on Monday July 31, 2006 @04:40PM (#15820348)

    I believe PHP has a bad reputation for three reasons:

    • It's a language used by a lot of beginners, so you see a lot of beginner code written in PHP.
    • It's historically had some really poor design decisions that have led people into making grave mistakes.
    • There are a number of better options available that people tend not to use because PHP has greater mindshare and therefore more hosting companies make it available. By comparison to the better options, PHP is crap.

    They are gradually fixing the language and the poor design decisions, but change in the language happens slowly, change in the developer attitudes happens even more slowly, and change in the perception of both of these happens glacially slowly.

    If you have control over your hosting arrangement, I'd strongly recommend trying out alternatives like Django (Python), Turbogears (Python) or Rails (Ruby) before committing to PHP any more than you already have. But if you need to work with bog-standard cheap web hosts, then stick with PHP, it's not that bad.

  • by KidSock ( 150684 ) on Monday July 31, 2006 @05:11PM (#15820598)
    Yeah, so PHP stands for "Personal Home Pages" but that's is an historical misnomer now. PHP has a CLI binary that can be used to run scripts on the commandline. Obligatory "hello world" follows:

        !#/usr/bin/php
        echo "Hello, world!";

    Now consider that PHP ships standard on virtually every Linux distro and comes with a large assortment of libraries. You can write LDAP scripts, do IMAP, generate images, the list is loooong. It amazes me that PHP isn't used more in corporate envirments. PHP is easy to use, arrays are surprisingly useful, and you can do a little OO (which is just the right amount IMO). And something that a lot of people take for granted is that the documentation on php.net is great. Everything is on one place unlike other languages (e.g. Python) where you just get redirected to every little sourceforge scribble and wiki there is.

    I'm a C person. I'll continue to use C for heavy lifting but you also need a good scripting language. I just wrote a Zend extension to interface with some of my C work and it exceeded all of my expectations.

    If you're looking for the lastest hot new "technology" then Ruby is a good buzzword. Otherwise, if you're just looking to get work done, so you can go home and play with your kids, PHP is a workhorse.

    PS: I don't know spit about this book but the tutorial on writing extensions on the Zend website was pretty good. Good enough for me anyway.
  • by weasello ( 881450 ) <weasel@gIIIreens ... inus threevowels> on Monday July 31, 2006 @05:25PM (#15820708) Homepage
    A lot of PHP bashing going on; I'd just like to chip in my 2 cents on the language (and demonstrate a mild interest in the book). I was big on programming when I was younger - by 14 I had written an adventure game in Basic and I invented a DOS-based graphical application that is eerily similar to Flash (two stickmen and some props on the screen with keyframes and interpolation tracking). Needless to say I was well advanced of my classmates throughout highschool. I also wrote a Chess AI (who hasn't?) in C. But that was the end of it - about 10 years ago now. I longed for programming but Real Life(tm) got in the way and other career paths curbed my free time. Needless to say I had lost a lot of skills and I don't even know what OOP stands for, but getting into the blogging world and creating a custom website to house it resulted in me having to learn some sort of web-based programming. I have to say that PHP was beautifully easy to (re-)learn and I was back in the programming seat with a big grin on my face with just a few weeks of self-learning (by looking at examples and open source, no books). I'm praising PHP as a very easy to learn, easy to use starting point for all my would-be programming friends.

I have hardly ever known a mathematician who was capable of reasoning. -- Plato

Working...