PHP extensions are automatically generated

Source: Internet
Author: User
Tags definition config function prototype header pear resource

Extension builds

There is no doubt that you have noticed that each PHP extension contains some very common and very monotonous structures and files. When we start a new extension development, if these public structures already exist, it is meaningful to consider populating the code. To do this, a simple but useful shell script is included in PHP.

Ext_skel

Switch to the ext/directory under your PHP source tree, and execute the following command:

jdoe@devbox:/home/jdoe/cvs/php-src/ext/$./ext_skel Extname=sample7

Just a minute, you can output some text, and you'll see the following output:

To use your new extension, you'll have to execute the following steps:  
      
1.  $ cd ...  
2.  $ VI ext/sample7/config.m4  
3.  $./buildconf  
4.  $./configure [With|enable]-sample7  
5.  $ make  
6.  $./php-f ext/sample7/sample7.php  
7.  $ VI ext/sample7/sample7.c  
8.  $ make  
      
Repeat steps 3-6 until to are satisfied with EXT/SAMPLE7/CONFIG.M4 and step  
6 confirms that your module is compiled into PHP. Then, start writing code and repeat the last two steps as often as necessary  
.

Looking at the Ext/sample7 directory now, you will see the annotated version of the extended skeleton code that you wrote in the 5th chapter, "Your first extension." Just now you can't compile it yet; But just make a few changes to CONFIG.M4 to get it working so you can avoid most of the work you did in chapter 5th.

Generating function prototypes

If you want to write a wrapper extension on a third-party library, then you have a description of the machine-scale version of the function prototype and the basic behavior (header file), by passing an additional parameter to the./ext_skel, which automatically scans your header file and creates a simple php_ corresponding to the interface Fucntion () block. The following is the use of the./ext_skel directive to parse the zlib header:

jdoe@devbox:/home/jdoe/cvs/php-src/ext/$./ext_skel extname=sample8 \
proto=/usr/local/include/zlib/zlib.h

Now in ext/sample8/sample8.c, you can see many php_function () definitions, one for each zlib function. Be aware that the Skeleton Builder generates warning messages for some unknown resource types. You need to pay special attention to these functions, and in order to associate these internal complex structures with the accessible variables of user space, you may need to use the knowledge you learned in the 9th chapter, "Resource data types."

Pecl_gen

There is a more sophisticated but also more complex code generator: Pecl_gen, which can be found in PECL (http://pecl.php.net) and can be installed using the Pear Install Pecl_gen command.

Translator Note: Pecl_gen has migrated to Codegen_pecl (Http://pear.php.net/package/CodeGen_PECL). This chapter involves code testing using CODEGEN_PECL version information as: "PHP 1.1.3, Copyright (c) 2003-2006 Hartmut Holzgraefe", if you have problems with the environment, please refer to the translator's environmental configuration.

Once the installation is complete, it can run like a Ext_skel, accept the same input parameters, produce roughly the same output, or if a complete XML definition file is provided, a more robust and fully-compiled version of the extension will be generated. Pecl_gen does not save you time to write extended core functionality; Instead, it provides an optional way to efficiently generate the extended skeleton code.

Specfile.xml

The following is the simplest extension definition file:

<?xml version= "1.0" encoding= "Utf-8"?> <extension name=  
"Sample9" >  
 <functions>  
  < function name= "Sample9_hello_world" role= "public" >  
   <code>  
<![ cdata[ 
     
    php_printf ("Hello world!"); 
] >
   </code>  
  </function>  
 </functions>  
</extension>

Please note that the translator uses the first line of the original book with fewer question marks, resulting in the inability to use, plus on OK.

Run this file through the Pecl_gen command:

jdoe@devbox:/home/jdoe/cvs/php-src/ext/$ Pecl-gen Specfile.xml

will produce an extension named Sample9 and expose a user space function Sample9_hello_world ().

About extension

In addition to the feature files you already know, Pecl_gen also produces a package.xml file that can be used for pear installation. If you plan to publish packages to the PECL library, or even if you just want to use the Pear package system to deliver content, this file will be useful.

In short, you can specify the elements of most package.xml files in the specfile.xml of Pecl_gen.

<?xml version= "1.0" encoding= "UTF-8"?> <extension name=  
"Sample9" >  
    <summary>extension 9 Generated by pecl_gen</summary>  
    <description>another sample of PHP Extension writing</description >  
    <maintainers>  
        <maintainer>  
            <name>john D. bookreader</name>  
            < email>jdb@example.com</email>  
            <role>lead</role>  
        </maintainer>  
    </ maintainers>  
    <release>  
        <version>0.1</version>  
        <date>2006-01-01</ date>  
        <state>beta</state>  
        <notes>initial release</notes>  
    </release > ...  
</extension>

When Pecl_gen creates an extension, the information is translated into the final package.xml file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.