PHP扩展开发

PHP 扩展位于 ext 文件夹,文件夹中有 ext_skel.php 可用来快速创建扩展

php ext_skel.php --help

WHAT IT IS

  It's a tool for automatically creating the basic framework for a PHP extension.

HOW TO USE IT

  Very simple. First, change to the ext/ directory of the PHP sources. Then run
  the following

    php ext_skel.php --ext extension_name

  and everything you need will be placed in directory ext/extension_name.

  If you don't need to test the existence of any external header files,
  libraries or functions in them, the extension is ready to be compiled in PHP.
  To compile the extension run the following:

    cd extension_name
    phpize
    ./configure
    make

  Don't forget to run tests once the compilation is done:

    make test

  Alternatively, to compile extension in the PHP:

    cd /path/to/php-src
    ./buildconf
    ./configure --enable-extension_name
    make
    make test TESTS=ext/extension_name/tests

  The definition of PHP_extension_NAME_VERSION will be present in the
  php_extension_name.h and injected into the zend_extension_entry definition.
  This is required by the PECL website for the version string conformity checks
  against package.xml

SOURCE AND HEADER FILE NAME

  The ext_skel.php script generates 'extension_name.c' and 'php_extension_name.h'
  as the main source and header files. Keep these names.

  extension functions (User functions) must be named

  extension_name_function()

  When you need to expose extension functions to other extensions, expose
  functions strictly needed by others. Exposed internal function must be named

  php_extension_name_function()

  See also CODING_STANDARDS.md.

OPTIONS

  php ext_skel.php --ext <name> [--experimental] [--author <name>]
                   [--dir <path>] [--std] [--onlyunix]
                   [--onlywindows] [--help]

  --ext <name>          The name of the extension defined as <name>
  --experimental        Passed if this extension is experimental, this creates
                        the EXPERIMENTAL file in the root of the extension
  --author <name>       Your name, this is used if --std is passed and for the
                        CREDITS file
  --dir <path>          Path to the directory for where extension should be
                        created. Defaults to the directory of where this script
                        lives
  --std                 If passed, the standard header used in extensions that
                        is included in the core, will be used
  --onlyunix            Only generate configure scripts for Unix
  --onlywindows         Only generate configure scripts for Windows
  --help                This help

执行 php ext_skel.php --ext extension_name 创建扩展  extension_name 替换为自己的扩展名称

php ext_skel.php --ext marin

Copying config scripts... done
Copying sources... done
Copying tests... done

Success. The extension is now ready to be compiled. To do so, use the
following steps:

cd /path/to/php-src/marin
phpize
./configure
make

Don't forget to run tests once the compilation is done:
make test

Thank you for using PHP!

此时在ext文件夹中可以发现多了 marin 文件夹

ll | grep marin

drwxr-xr-x    8 tal  staff   256B Dec 28 14:59 marin

marin 文件夹中文件如下

-rw-r--r--  1 tal  staff   3.2K Dec 28 14:59 config.m4
-rw-r--r--  1 tal  staff   204B Dec 28 14:59 config.w32
-rw-r--r--  1 tal  staff   2.2K Dec 28 14:59 marin.c
-rw-r--r--  1 tal  staff   309B Dec 28 14:59 php_marin.h
drwxr-xr-x  5 tal  staff   160B Dec 28 14:59 tests

marin.c文件中定义了两个示例函数 marin_test1 marin_test2

/* {{{ void marin_test1()
 * 无返回值函数
 */
PHP_FUNCTION(marin_test1)
{
	ZEND_PARSE_PARAMETERS_NONE();

	php_printf("The extension %s is loaded and working!\r\n", "marin");
}
/* }}} */

/* {{{ string marin_test2( [ string $var ] )
 * 接收一个可选参数,有返回值
 */
PHP_FUNCTION(marin_test2)
{
	char *var = "World";
	size_t var_len = sizeof("World") - 1;
	zend_string *retval;

	ZEND_PARSE_PARAMETERS_START(0, 1)
		Z_PARAM_OPTIONAL
		Z_PARAM_STRING(var, var_len)
	ZEND_PARSE_PARAMETERS_END();

	retval = strpprintf(0, "Hello %s", var);

	RETURN_STR(retval);
}

编译一下看看效果

在marin文件夹中执行

phpize
./configure
make
make test
make install

然后在php.ini开启marin扩展

创建index.php

<?php
marin_test1();

 运行文件

php test.php

The extension marin is loaded and working!

编辑index.php

<?php
echo marin_test2();
echo "\n";
echo marin_test2("marin");

运行文件

php test.php

Hello World
Hello marin%

创建一个求两个整数和的函数

编辑marin.c

PHP_FUNCTION(marin_test1)
{
	ZEND_PARSE_PARAMETERS_NONE();

	php_printf("The extension %s is loaded and working!\r\n", "marin");
}
/* }}} */

/* {{{ string marin_test2( [ string $var ] )
 */
PHP_FUNCTION(marin_test2)
{
	char *var = "World";
	size_t var_len = sizeof("World") - 1;
	zend_string *retval;

	ZEND_PARSE_PARAMETERS_START(0, 1)
		Z_PARAM_OPTIONAL
		Z_PARAM_STRING(var, var_len)
	ZEND_PARSE_PARAMETERS_END();

	retval = strpprintf(0, "Hello %s", var);

	RETURN_STR(retval);
}

// 新增代码
PHP_FUNCTION(marin_test3){
    int a;
    int b;
    ZEND_PARSE_PARAMETERS_START(2, 2)
        Z_PARAM_LONG(a)
        Z_PARAM_LONG(b)
    ZEND_PARSE_PARAMETERS_END();

    RETURN_LONG(a+b);
}

...
...
...

static const zend_function_entry marin_functions[] = {
	PHP_FE(marin_test1,		arginfo_marin_test1)
	PHP_FE(marin_test2,		arginfo_marin_test2)
    PHP_FE(marin_test3,		NULL)  // 新增代码
	PHP_FE_END
};

重新编译

phpize
./configure
make
make test
make install
 

编辑index.php

<?php
echo marin_test3(1,2);

运行文件 

php test.php 

3%                                 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值