C++
  Home arrow C++ arrow Page 4 - Developing Custom PHP Extensions: Part 1
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Developing Custom PHP Extensions: Part 1
By: Igal Raizman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 45
    2002-07-18

    Table of Contents:
  • Developing Custom PHP Extensions: Part 1
  • What is a PHP extension?
  • Setting up our Development environment
  • Our First Extension
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Developing Custom PHP Extensions: Part 1 - Our First Extension


    (Page 4 of 5 )

    If you followed all of the instructions carefully then you should be able to commence writing your own extensions. I will leave all the serious coding to the next article and just show you a basic extension that prints "Hello World" five times. Copy and paste the following code into your project; compile and build it:

    /* include standard header */
    /* you will need to include this in all of your php extension projects*/
    #include "php.h"

    /* All the functions that will be exported (available) must be declared */
    ZEND_FUNCTION(hello_world);
    PHP_MINFO_FUNCTION(devarticlesmod);

    /* Just a basic int to be used as a counter*/
    int i;

    /* function list so that the Zend engine will know what’s here */
    zend_function_entry devarticlesmod_functions[] =
    {
    ZEND_FE(hello_world, NULL)
    {NULL, NULL, NULL}
    };

    /* module information */
    zend_module_entry devarticlesmod_module_entry =
    { STANDARD_MODULE_HEADER,
    "DevArticles",
    devarticlesmod_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    PHP_MINFO(devarticlesmod),
    NO_VERSION_YET,
    STANDARD_MODULE_PROPERTIES };

    #if COMPILE_DL_DEVARTICLES_MOD
    ZEND_GET_MODULE(devarticlesmod)
    #endif

    PHP_MINFO_FUNCTION(devarticlesmod)
    {
    php_info_print_table_start();
    php_info_print_table_row(2, "DevArticles Extension", "All Systems Go");
    php_info_print_table_end();
    }

    ZEND_FUNCTION(hello_world)
    {

    for(i=0;i<5;i++)
    {
    zend_printf("Hello World<br>");
    }
    }


    Don't worry if you do not understand a lot of what's in the C code above. This article is only meant to show you how to setup the environment and explain how to use the extensions in your scripts. All of the code-related material will be explained in the subsequent article(s).

    After you've compiled and built the extension, you should take the newly created .dll file (php_devarticlesmod.dll in our case) and place it in your web-server directory. In that same directory, create a new PHP file and type the following:

    <?php

    dl("php_devarticlesmod.dll");

    hello_world();

    phpinfo();

    ?>


    The first line tells php to load the module/extension. The function takes the name of the extension file as the parameter.

    The second line, hello_world(), is the function we created in our extension. Upon calling this function you should see "Hello World" printed five times on the screen.

    And, lastly, phpinfo() is called. If you scroll down through the output you should see a confirmation that your extension is indeed loaded:

    Making sure our extension has loaded

    More C++ Articles
    More By Igal Raizman


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek