# 26 SWIG and PHP4

- Generating PHP4 Extensions
  - Building a loadable extension
  - Building extensions into PHP
  - Using PHP4 Extensions
- Basic PHP4 interface
  - Constants
  - Global Variables
  - Functions
  - Overloading
  - Pointers and References
  - Structures and C++ classes
    - Using `-noproxy`
    - Constructors and Destructors
    - Static Member Variables
    - Static Member Functions
  - PHP4 Pragmas, Startup and Shutdown code

Caution: This chapter (and module!) is still under construction

In this chapter, we discuss SWIG's support of PHP4. The PHP4 module has been extensively rewritten in release 1.3.26. Although it is significantly more functional, it still does not implement all the features available in other languages.

The examples and test cases have been developed with PHP4. Support for PHP5 at this time is limited to wrapping C libraries or C++ libraries while using the `-noproxy` flag. This deficiency will be fixed in a subsequent release of SWIG.

In order to use this module, you will need to have a copy of the PHP 4.0 (or above) include files to compile the SWIG generated files. You can find these files by running `'php-config --includes'`. To test the modules you will need either the php binary or the Apache php module. If you want to build your extension into php directly (without having the overhead of loading it into each script), you will need the complete PHP source tree available.

## 26.1 Generating PHP4 Extensions

To build a PHP4 extension, run swig using the `-php4` option as follows :

    swig -php4 example.i

This will produce 3 files example_wrap.c, php_example.h and example.php. The first file, `example_wrap.c` contains all of the C code needed to build a PHP4 extension. The second file, `php_example.h` contains the header information needed to statically link the extension into PHP. The third file, `example.php` can be included by php scripts. It attempts to dynamically load the extension and contains extra php code specified in the interface file.

Swig can generate PHP4 extensions from C++ libraries as well when given the `-c++` option. The support for C++ is discussed in more detail in section 24.2.6.

To finish building the extension, you have two choices. You can either build the extension as a separate shared object file which will then have to be explicitly loaded by each script. Or you can rebuild the entire php source tree and build the extension into the php executable/library so it will be available in every script. The first choice is the default, however it can be changed by passing the '-phpfull' command line switch to select the second build method.

### 26.1.1 Building a loadable extension

There are two methods to build the extension as a dynamically loaded module: using standard compilation utilities (make, gcc), or using PHP4's phpize utility.

To build manually, use a compile string similar to this (different for each OS):

    	cc -I.. $(PHPINC) -fpic -c example_wrap.c
    	cc -shared example_wrap.o -o libexample.so

The `-make` command line argument to swig will generate an additional file Makefile. This Makefile can usually build the extension itself (on unix platforms).

If you want to build your extension using the `phpize` utility, or if you want to build your module into PHP directly, you can specify the `-phpfull` command line argument to swig.

The `-phpfull` will generate three additional files. The first extra file, `config.m4` contains the shell code needed to enable the extension as part of the PHP4 build process. The second extra file, `Makefile.in` contains the information needed to build the final Makefile after substitutions. The third and final extra file, `CREDITS` should contain the credits for the extension.

To build with phpize, after you have run swig you will need to run the 'phpize' command (installed as part of php) in the same directory. This re-creates the php build environment in that directory. It also creates a configure file which includes the shell code from the config.m4 that was generated by SWIG, this configure script will accept a command line argument to enable the extension to be run ( by default the command line argument is --enable-modulename, however you can edit the config.m4 file before running phpize to accept --with-modulename. You can also add extra tests in config.m4 to check that a correct library version is installed or correct header files are included, etc, but you must edit this file before running phpize. ) If you like SWIG can generate simple extra tests for libraries and header files for you.

    	swig -php4 -phpfull

If you depend on source files not generated by SWIG, before generating the configure file, you may need to edit the `Makefile.in` file. This contains the names of the source files to compile (just the wrapper file by default) and any additional libraries needed to be linked in. If there are extra C files to compile, you will need to add them to the Makefile.in, or add the names of libraries if they are needed. In simple cases SWIG is pretty good at generating a complete Makefile.in and config.m4 which need no further editing.

You then run the configure script with the command line argument needed to enable the extension. Then run make, which builds the extension. The extension object file will be left in the modules sub directory, you can move it to wherever it is convenient to call from your php script.

Both the `-make` and `-phpfull` arguments accept additional optional arguments:

- `-withincs "<files>"` Adds include files to the config.m4 file.
- `-withlibs "<files>"` Links the libraries into the shared object.
- `-withc "<files>"` Compiles and links the named C files into the shared object.
- `-withcxx "<files>"` Compiles and links the named C++ files into the shared object,
- `-dlname "<name>"` Changes the name of the generated shared object.

### 26.1.2 Building extensions into PHP

This method, selected with the `-phpfull` command line switch, involves rebuilding the entire php source tree. Whilst more complicated to build, it does mean that the extension is then available without having to load it in each script.

After running swig with the -phpfull switch, you will be left with a shockingly similar set of files to the previous build process. However you will then need to move these files to a subdirectory within the php source tree, this subdirectory you will need to create under the ext directory, with the name of the extension ( e.g mkdir php-4.0.6/ext/modulename .)

After moving the files into this directory, you will need to run the 'buildall' script in the php source directory. This rebuilds the configure script and includes the extra command line arguments from the module you have added.

Before running the generated configure file, you may need to edit the `Makefile.in`. This contains the names of the source files to compile ( just the wrapper file by default) and any additional libraries needed to link in. If there are extra C files to compile you will need to add them to the Makefile, or add the names of libraries if they are needed. In most cases `Makefile.in` will be complete, especially if you make use of `-withlibs` and `-withincs`

    	swig -php4 -phpfull -withlibs "xapian omquery" --withincs "om.h"

Will include in the config.m4 and Makefile.in search for libxapian.a or libxapian.so and search for libomquery.a or libomquery.so as well as a search for om.h

You then need to run the configure command and pass the necessary command line arguments to enable your module ( by default this is --enable-modulename, but this can be changed by editing the config.m4 file in the modules directory before running the buildall script. In addition, extra tests can be added to the config.m4 file to ensure the correct libraries and header files are installed.)

Once configure has completed, you can run make to build php. If this all compiles correctly, you should end up with a php executable/library which contains your new module. You can test it with a php script which does not have the 'dl' command as used above.

### 26.1.3 Using PHP4 Extensions

To test the extension from a PHP script, you need to load it first. You do this by putting the line,

    	dl("/path/to/modulename.so");	// Load the module

at the start of each PHP file. SWIG also generates a php module, which attempts to do the `dl()` call for you:

    	include("example.php");

## 26.2 Basic PHP4 interface

It is important to understand that PHP uses a single global namespace into which all symbols from extension modules are loaded. It is quite possible for names of symbols in one extension module to clash with other symbols unless care is taken to `%rename` them.

### 26.2.1 Constants

These work in much the same way as in C/C++, constants can be defined by using either the normal C pre-processor declarations, or the `%constant` SWIG directive. These will then be available from your PHP script as a PHP constant, (i.e. no dollar sign is needed to access them. ) For example, with a swig file like this,

    %module example

    #define PI 3.14159

    %constant int E  = 2.71828

you can access the constants in your php script like this,

    include("example.php");

    echo "PI = " . PI . "\n";

    echo "E = " . E . "\n";

There are two peculiarities with using constants in PHP4. The first is that if you try to use an undeclared constant, it will evaluate to a string set to the constant's name. For example,

    %module example

    #define EASY_TO_MISPELL	0

accessed incorrectly in PHP,

    include("example.php");

    if(EASY_TO_MISPEL) {
    	....
    } else {
    	....
    }

will issue a warning about the undeclared constant, but will then evaluate it and turn it into a string ('EASY_TO_MISPEL'), which evaluates to true, rather than the value of the constant which would be false. This is a feature.

The second 'feature' is that although constants are case sensitive (by default), you cannot declare a constant twice with alternative cases. E.g.,

    %module example

    #define TEST	Hello
    #define Test	World

accessed from PHP,

    include("example.php");

    echo TEST, Test;

will output "Hello Test" rather than "Hello World". This is because internally, all constants are stored in a hash table by their lower case name, so 'TEST' and 'Test' will map to the same hash element ('Test'). But, because we declared them case sensitive, the Zend engine will test if the case matches with the case the constant was declared with first.

So, in the example above, the TEST constant was declared first, and will be stored under the hash element 'test'. The 'Test' constant will also map to the same hash element 'test', but will not overwrite it. When called from the script, the TEST constant will again be mapped to the hash element 'test' so the constant will be retrieved. The case will then be checked, and will match up, so the value ('Hello') will be returned. When 'Test' is evaluated, it will also map to the same hash element 'test'. The same constant will be retrieved, this time though the case check will fail as 'Test' != 'TEST'. So PHP will assume that Test is a undeclared constant, and as explained above, will return it as a string set to the constant name ('Test'). Hence the script above will print 'Hello Test'. If they were declared non-case sensitive, the output would be 'Hello Hello', as both point to the same value, without the case test taking place. ( Apologies, this paragraph needs rewriting to make some sense. )

### 26.2.2 Global Variables

Because PHP4 does not provide a mechanism to intercept access and assignment of global variables, global variables are supported through the use of automatically generated accessor functions.

    %module example;

    %inline %{
      double seki = 2;
      void print_seki() {
        zend_printf("seki is now %f\n",seki);
      }
    %}

is accessed as follows:

    include("example.php");
    print seki_get();
    seki_set( seki_get() * 2);	# The C variable is now 4.
    print seki_get();

SWIG supports global variables of all C datatypes including pointers and complex objects. Additional types can be supported by using the `varinit` typemap.

SWIG honors the `%immutable` modifier by not generating code for the `_set` method. This provides read-only access to the variable from the php script. Attempting to access the `_set` method will result in a php fatal error because the function is undefined.

At this time SWIG does not support custom accessor methods.

### 26.2.3 Functions

C functions are converted into PHP functions. Default/optional arguments are also allowed. An interface file like this :

    %module example
    int foo(int a);
    double bar(double, double b = 3.0);
    ...

Will be accessed in PHP like this :

    include("example.php");
    $a = foo(2);
    $b = bar(3.5, -1.5);
    $c = bar(3.5);		# Use default argument for 2nd parameter

Because PHP4 is a dynamically typed language, the default typemaps used for simple types will attempt to coerce the arguments into the appropriate type. That is the following invocations are equivalent:

    $a = foo(2);
    $a = foo("2");
    $a = foo(2.0);

Functions are invoked using pass by value semantics like all of PHP. This means the conversion which automatically takes place when invoking a swig wrapped method does not change the native type of the argument variable.

    $s = "2 A string representing two";
    $a = foo($s);  # invokes 'foo(2)';
    print $s;      # The value of $s was not changed.

### 26.2.4 Overloading

Although PHP4 does not support overloading functions natively, swig will generate dispatch functions which will use `%typecheck` typemaps to allow overloading. This dispatch function's operation and precedence is described in Wrapping Overloaded Functions and Methods.

Because PHP4 is a dynamically typed language, simple values can be silently converted from one type to another. For example, integers, doubles and strings silently convert to each other depending on context. This situation make overloading slightly problematic because given the following function:

    void doit( int i );
    void doit( double i );

it is questionable which to invoke when `doit("2");` is used in PHP. The string `"2"` simultaneously represents the integer `2` and the double `2.0`.

In order to provide the most natural experience to PHP programmers, the default `%typecheck` implemented in `php4.swg` allows any simple type (integer, double, string) in PHP to be used for any simple C type (int, double, char *). The function selected then depends only on the argument type precedence defined by SWIG.

It should be noted that `SWIGTYPE` references and pointers will not be silently converted. So these two functions:

    void doit( const Vector & );
    void doit( int i );

Cause less confusion and `doit("2");` will invoke the function taking the integer argument.

### 26.2.5 Pointers and References

Pointers to C/C++ objects are no longer represented as character strings such as: `_523d3f4_Circle_p`, instead they are represented as PHP resources, rather like MySQL connection handles.

There are multiple ways to wrap pointers to simple types. Given the following C method:

      void add( int *in1, int *in2, int *result);

One can include cpointer.i to generate PHP wrappers to `int *`.

    %module example
    %include cpointer.i
    %pointer_functions(int,intp)

    void add( int *in1, int *in2, int *result);

This will result in the following usage in PHP:

    <?php

    include("example.php");

    $in1=copy_intp(3);
    $in2=copy_intp(5);
    $result=new_intp();

    add( $in1, $in2, $result );

    echo "The sum " . intp_value($in1) . " + " . intp_value($in2) . " = " . intp_value( $result) . "\n";
    ?>

An alternative would be to use the include typemaps.i which defines named typemaps for INPUT, OUTPUT and INOUT variables. One needs to either `%apply` the appropriate typemap or adjust the parameter names as appropriate.

    %module example
    %include typemaps.i

    void add( int *INPUT, int *INPUT, int *OUTPUT);

This will result in the following usage in PHP:

    <?php

    include("example.php");

    $in1 = 3;
    $in2 = 5;
    $result= add($in1,$in2);  # Note using variables for the input is unnecessary.

    echo "The sum $in1 + $in2 = $result\n";
    ?>

Because PHP has a native concept of reference, it may seem more natural to the PHP developer to use references to pass pointers. To enable this, one needs to include phppointers.i which defines the named typemap REFERENCE.

    %module example
    %include phppointers.i

    void add( int *REF, int *REF, int *REF);

This will result in the following usage in PHP:

    <?php

    include("example.php");

    $in1 = 3;
    $in2 = 5;
    $result = 0;
    add(&$in1,&$in2,&$result);

    echo "The sum $in1 + $in2 = $result\n";
    ?>

It is important to note that a php variable which is NULL when passed by reference would end up passing a NULL pointer into the function. In PHP, an unassigned variable (ie first reference is not assigned) is NULL. In the above example, if any of the three variables had not been assigned, a NULL pointer would have been passed into `add`. Depending on the implementation of the function, this may or may not be a good thing.

We chose to allow passing NULL pointers into functions because that is sometimes required in C libraries. A NULL pointer can be created in PHP in a number of ways: by using `unset` on an existing variable, or assigning `NULL` to a variable.

### 26.2.6 Structures and C++ classes

By default, SWIG represents structs and C++ classes using a PHP4 class. The PHP4 class is implemented entirely using the Zend C API so no additional php code is generated.

This interface file

    %module vector

    class Vector {
    public:
    	double x,y,z;
    	Vector();
    	~Vector();
    	double magnitude();
    };

    struct Complex {
     double re, im;
    };

Would be used in the following way:

    <?php
      require "vector.php";

      $v = new Vector();
      $v->x = 3;
      $v->y = 4;
      $v->z = 5;

      echo "Magnitude of ($v->x,$v->y,$v->z) = " . $v->magnitude() . "\n";

      $v = NULL;   # destructor called.

      $c = new Complex();

      $c->re = 0;
      $c->im = 0;

      # $c destructor called when $c goes out of scope.
    ?>

Member variables and methods are accessed using the `->` operator.

#### 26.2.6.1 Using `-noproxy`

The `-noproxy` option flattens the object structure and generates collections of named functions. The above example results in the following PHP functions:

    new_Vector();
    Vector_x_set($obj,$d);
    Vector_x_get($obj);
    Vector_y_set($obj,$d);
    Vector_y_get($obj);
    Vector_z_set($obj,$d);
    Vector_z_get($obj);
    Vector_magnitude($obj);
    new_Complex();
    Complex_re_set($obj,$d);
    Complex_re_get($obj);
    Complex_im_set($obj,$d);
    Complex_im_get($obj);

#### 26.2.6.2 Constructors and Destructors

The constructor is called when `new Object()` (or `new_Object()` if using `-noproxy`) is used to create an instance of the object. If multiple constructors are defined for an object, function overloading will be used to determine which constructor to execute.

Because PHP4 uses reference counting to manage resources, simple assignment of one variable to another such as:

    $ref = $v;

causes the symbol `$ref` to refer to the same underlying object as `$v`. This does not result in a call to the C++ copy constructor or copy assignment operator.

One can force execution of the copy constructor by using:

    $o_copy = new Object($o);

Destructors are automatically called when all variables referencing the instance are reassigned or go out of scope. The destructor is not available to be called manually. To force a destructor to be called the programmer can either reassign the variable or call `unset($v)`

#### 26.2.6.3 Static Member Variables

Class variables are not supported in PHP. Static member variables are therefore accessed using a class function with the same name, which returns the current value of the class variable. For example

    %module example

    class Ko {
    	static int threats;
    };

would be accessed in PHP as,

    include("example.php");

    echo "There has now been " . Ko::threats() . " threats\n";

To set the static member variable, pass the value as the argument to the class function, e.g.

    Ko::threats(10);

    echo "There has now been " . Ko::threats() . " threats\n";

#### 26.2.6.4 Static Member Functions

Class functions are supported in PHP using the `class::function()` syntax. For example

    %module example
    class Ko {
      static void threats();
    };

would be executed in PHP as,

    include("example.php");
    Ko::threats();

### 26.2.7 PHP4 Pragmas, Startup and Shutdown code

To place PHP code in the generated "example.php" file one can use the code pragma. The code is inserted after loading the shared object.

    %module example
    %pragma(php4) code="
    # This code is inserted into example.php
    echo \"example.php execution\\n\";
    "

Results in the following in "example.php"

    # This code is inserted into example.php
    echo "example.php execution\n";

The include pragma is a short cut to add include statements to the example.php file.

    %module example
    %pragma(php4) code="
    include \"include.php\";
    "
    %pragma(php) include="include.php"   // equivalent.

The phpinfo pragma inserts code in the `PHP_MINFO_FUNCTION` which is called from PHP's phpinfo() function.

    %module example;
    %pragma(php4) phpinfo="
      zend_printf("An example of PHP support through SWIG\n");
      php_info_print_table_start();
      php_info_print_table_header(2, \"Directive\", \"Value\");
      php_info_print_table_row(2, \"Example support\", \"enabled\");
      php_info_print_table_end();
    "

To insert code into the `PHP_MINIT_FUNCTION`, one can use either `%init` or `%minit`.

    %module example;
    %init {
      zend_printf("Inserted into PHP_MINIT_FUNCTION\n");
    }
    %minit {
      zend_printf("Inserted into PHP_MINIT_FUNCTION\n");
    }

To insert code into the `PHP_MSHUTDOWN_FUNCTION`, one can use either `%init` or `%minit`.

    %module example;
    %mshutdown {
      zend_printf("Inserted into PHP_MSHUTDOWN_FUNCTION\n");
    }

The `%rinit` and `%rshutdown` statements insert code into the request init and shutdown code respectively.