Building and Compiling a Host Application

By itself, a library is just a collection of code with no purpose. In order to "make it go," you'll need something to embed PHP into. To begin, let's put together a very simple wrapper application that starts up the Zend Engine and initializes PHP to handle a request, and then reverses the process to unwind the stack and clean up resources (see Listing 19.1).

Listing 19.1. A Simple embed1.c Application

#include 

int main(int argc, char *argv[])
{
 PHP_EMBED_START_BLOCK(argc,argv)
 PHP_EMBED_END_BLOCK()

 return 0;
}

Because so many header files are involved, building actually requires a longer command than this simple code snippet would suggest. If you used a different EPREFIX location than the default (/usr/local), be sure to substitute that location in the following example:

$ gcc -o embed1 embed1.c 
 -I/usr/local/include/php/ 
 -I/usr/local/include/php/main 

 -I/usr/local/include/php/Zend 
 -I/usr/local/include/php/TSRM 
 -L/usr/local/lib -lphp5

Because this command will become a hassle to type over and over again, you might prefer to use a simple Makefile instead (see Listing 19.2).

Listing 19.2. Reducing the Work with a Makefile

CC = gcc
CFLAGS = -c -I/usr/local/include/php/ 
 -I/usr/local/include/php/main 
 -I/usr/local/include/php/Zend 
 -I/usr/local/include/php/TSRM 
 -Wall -g
LDFLAGS = -L/usr/local/lib -lphp5

all: embed1.c
 $(CC) -o embed1.o embed1.c $(CFLAGS)
 $(CC) -o embed1 embed1.o $(LDFLAGS)

Note

This Makefile differs from the earlier command provided in a few important ways. First, it enables compile-time warnings with the -Wall switch, and adds debugging information with -g. It also splits the compilation and linking stages into two separate pieces to make it easier to add more source files later on. Feel free to reorganize this Makefile to suit your personal tastes; just be sure to use tabs for indentation here, not spaces.

Now, as you make changes to your embed1.c source file, you'll be able to rebuild the embed1 executable with just a simple make command.


Re creating CLI by Wrapping Embed

The PHP Life Cycle

Variables from the Inside Out

Memory Management

Setting Up a Build Environment

Your First Extension

Returning Values

Accepting Parameters

Working with Arrays and HashTables

The Resource Data Type

PHP4 Objects

PHP5 Objects

Startup, Shutdown, and a Few Points in Between

INI Settings

Accessing Streams

Implementing Streams

Diverting the Stream

Configuration and Linking

Extension Generators

Setting Up a Host Environment

Advanced Embedding

Appendix A. A Zend API Reference

Appendix B. PHPAPI

Appendix C. Extending and Embedding Cookbook

Appendix D. Additional Resources



Extending and Embedding PHP
Extending and Embedding PHP
ISBN: 067232704X
EAN: 2147483647
Year: 2007
Pages: 175
Authors: Sara Golemon

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net