PHP4 bindings for Xapian

The PHP4 bindings for Xapian are packaged in the xapian.so extension, and largely follow the C++ API. This document lists the differences and additions. PHP4 strings and arrays, etc., are converted automatically in the bindings, so generally it should just work as expected.

The examples subdirectory contains examples showing how to use the PHP4 bindings based on the simple examples from xapian-examples: simpleindex.php4, simplesearch.php4.

Installation

Assuming you have suitable versions of PHP4 and SWIG installed, running configure will automatically enable the PHP4 bindings, and make install will install the extension shared library in the location reported by php-config --extension-dir.

Check that php.ini has a line like extension_dir = "<location reported by php-config --extension-dir>".

Then add this line to php.ini: extension = xapian.so (or whatever the library is called - not all UNIX systems use .so as the extension, and MS Windows uses .dll).

Depending how PHP is setup, you may need to restart the webserver for this change to take effect.

Alternatively you can add the following line to the start of your PHP scripts: dl('xapian.so');

Exceptions

In general, a PHP4 fatal error is triggered on a C++ exception being thrown in the library. This is awful, and should be fixed in some useful way - the intention is to rework the core library to not throw exceptions but this is not at trivial job.

Object orientation

The PHP4 bindings do not currently use a PHP4 object orientated style. In order to construct an object, use $object = new_ClassName(...). Destroy it using delete_ClassName($object). Invoke methods on an object using ClassName_method_name($object, ...).

Iterators

All iterators support next() and equals() methods to move through and test iterators (as for all language bindings). MSetIterator and ESetIterator also support prev().

Iterator dereferencing

C++ iterators are often dereferenced to get information, eg (*it). With PHP4 these are all mapped to named methods, as follows:

IteratorDereferencing method
PositionIterator get_termpos()
PostingIterator get_docid()
TermIterator get_term()
ValueIterator get_value()
MSetIterator get_docid()
ESetIterator get_termname()

Other methods, such as MSetIterator.get_document(), are available unchanged.

MSet

MSet objects have some additional methods to simplify access (these work using the C++ array dereferencing):

Method nameExplanation
get_hit(index)returns MSetIterator at index
get_document_percentage(index)convert_to_percent(get_hit(index))
get_document(index)get_hit(index).get_document()
get_document_id(index)get_hit(index).get_docid()

Database Factory Functions

Constants

Constants are wrapped with the Xapian namespace removed, so Xapian::DB_CREATE_OR_OPEN is available as DB_CREATE_OR_OPEN, Xapian::Query::OP_OR is available as Query_OP_OR, and so on.

Enquire

There is an additional method get_matching_terms() which takes an MSetIterator and returns a list of terms in the current query which match the document given by that iterator. You may find this more convenient than using the TermIterator directly.

Last updated $Date: 2004-12-08 16:14:33 +0000 (Wed, 08 Dec 2004) $