21.5 Adding a Function to PHP


You've written yourself a nifty little function and now you want to call it from PHP.

Technique

Let's take the function we wrote in recipe 21.1, and discuss the process of adding it.

  1. Place the source of your function in the correct file; for sample_function() , we place it in basic_functions.c.

  2. Find the place at the top of the file to which you added the function where it says something like the following. (Note that if you placed your function in ext/standard, look at the top of the file basic_functions.c.)

     function_entry MODULENAME_functions[] = {     PHP_FE(somefunction,        NULL)     ... } 

    Add a new line and place the following on that line:

     PHP_FE(sample_function,        NULL) 

    Now it should look like this:

     function_entry MODULENAME_functions[] = {     PHP_FE(somefunction,        NULL)     PHP_FE(sample_function,     NULL) /* You just added this */     ... } 
  3. Go to the header file for that file. The header file is usually named php_MODULENAME.h. You should see a bunch of declarations like this:

     PHP_FUNCTION(somefunction); 

    Add the following declaration:

     PHP_FUNCTION(sample_function); 
  4. Recompile and install PHP and voil   ”your own PHP function.

Comments

The preceding steps are how to add a new function to PHP. The concepts behind adding a function will be discussed in recipe 21.9, when we discuss creating a new PHP module. Adding a function ties in closely with creating a module.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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