这个例子需要一个扩展程序。
<?php
function hello_world() {
return 'Hello World';
}
?>
PHP_ARG_ENABLE(hello, whether to enable Hello
World support,
[
if test "$PHP_HELLO" = "yes"; then
AC_DEFINE(HAVE_HELLO, 1, [Whether you have Hello World])
PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi
#ifndef PHP_HELLO_H
#define PHP_HELLO_H 1
#define PHP_HELLO_WORLD_VERSION "1.0"
#define PHP_HELLO_WORLD_EXTNAME "hello"
PHP_FUNCTION(hello_world);
extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_hello.h"
static function_entry hello_functions[] = {
PHP_FE(hello_world, NULL)
{NULL, NULL, NULL}
};
zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_HELLO_WORLD_EXTNAME,
hello_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
PHP_HELLO_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif
PHP_FUNCTION(hello_world)
{
RETURN_STRING("Hello World", 1);
}
构建您的扩展
$ phpize
$ ./configure --enable-hello
$ make
运行这些命令后,您应该有一个hello.so文件
在php.ini中添加extension=hello.so以触发它。
php -r 'echo hello_world();'
你已经完成了。;-)
在这里阅读更多内容。
对于那些没有太多知识,想尝试使用zephir-lang来构建php扩展的人来说,这是一种简单的方法。
namespace Test;
class Hello
{
public function say()
{
echo "Hello World!";
}
}
使用Zephir编译它并获得测试扩展。