To build it:
zig build
phpize
./configure
make
To test it:
php -d extension=./modules/my_php_extension.so -r "echo hello_world();"
Should output:
Hello from ZIG!
const std = @import("std"); | |
pub fn build(b: *std.Build) void { | |
const optimize = b.standardOptimizeOption(.{}); | |
const target = b.standardTargetOptions(.{}); | |
const library = b.addStaticLibrary(.{ | |
.name = "my_php_extension", | |
.root_source_file = .{ | |
.path = "hello.zig", | |
}, | |
.target = target, | |
.optimize = optimize, | |
}); | |
library.addIncludePath(.{ | |
.path = "/usr/include/php/20220829" | |
}); | |
library.addIncludePath(.{ | |
.path = "/usr/include/php/20220829/main" | |
}); | |
library.addIncludePath(.{ | |
.path = "/usr/include/php/20220829/TSRM" | |
}); | |
library.addIncludePath(.{ | |
.path = "/usr/include/php/20220829/Zend" | |
}); | |
library.addIncludePath(.{ | |
.path = "/usr/include" | |
}); | |
library.addIncludePath(.{ | |
.path = "/usr/include/x86_64-linux-gnu" | |
}); | |
b.installArtifact(library); | |
} | |
// php-config --includes |
PHP_ARG_ENABLE(my_php_extension, whether to enable My PHP Extension, | |
[ --enable-my-php_extension Enable My PHP Extension support]) | |
if test "$PHP_MY_PHP_EXTENSION" != "no"; then | |
PHP_REQUIRE_CXX() | |
PHP_ADD_LIBRARY(stdc++, 1, MY_PHP_EXTENSION_SHARED_LIBADD) | |
PHP_SUBST(MY_PHP_EXTENSION_SHARED_LIBADD) | |
PHP_ADD_LIBRARY_WITH_PATH(my_php_extension, $(pwd)/zig-out/lib, MY_PHP_EXTENSION_SHARED_LIBADD) | |
PHP_NEW_EXTENSION(my_php_extension, hello.c, $ext_shared) | |
fi |
#include "hello.h" | |
PHP_MINIT_FUNCTION(my_php_extension) { | |
return SUCCESS; | |
} | |
PHP_MSHUTDOWN_FUNCTION(my_php_extension) { | |
return SUCCESS; | |
} | |
PHP_RINIT_FUNCTION(my_php_extension) { | |
return SUCCESS; | |
} | |
PHP_RSHUTDOWN_FUNCTION(my_php_extension) { | |
return SUCCESS; | |
} | |
PHP_MINFO_FUNCTION(my_php_extension) { | |
php_info_print_table_start(); | |
php_info_print_table_header(2, "My PHP Extension", "enabled"); | |
php_info_print_table_end(); | |
} | |
zend_module_entry my_php_extension_module_entry = { | |
STANDARD_MODULE_HEADER, | |
"my_php_extension", | |
myextension_functions, | |
PHP_MINIT(my_php_extension), | |
PHP_MSHUTDOWN(my_php_extension), | |
PHP_RINIT(my_php_extension), | |
PHP_RSHUTDOWN(my_php_extension), | |
PHP_MINFO(my_php_extension), | |
PHP_MY_PHP_EXTENSION_VERSION, | |
STANDARD_MODULE_PROPERTIES | |
}; | |
ZEND_GET_MODULE(my_php_extension) |
#ifndef MYEXTENSION_H | |
#define MYEXTENSION_H | |
#define PHP_MY_PHP_EXTENSION_VERSION "1.0.0" | |
#include "php.h" | |
extern zend_function_entry myextension_functions[]; | |
#endif // MYEXTENSION_H |
const std = @import("std"); | |
const php = @cImport({ | |
@cInclude("php.h"); | |
}); | |
export fn hello_world(execute_data: ?*php.zend_execute_data, return_value: ?*php.zval) void { | |
_ = execute_data; | |
_ = return_value; | |
_ = php.php_printf("Hello from ZIG!\n"); | |
} | |
const arg_info = [_]php.zend_internal_arg_info{ | |
.{ | |
.name = null, | |
.type = .{ | |
.type_mask = php.MAY_BE_NULL, | |
.ptr = null, | |
}, | |
}, | |
}; | |
export const myextension_functions = [_]php.zend_function_entry{ | |
php.zend_function_entry{ | |
.fname = "hello_world", | |
.handler = hello_world, | |
.arg_info = &arg_info, | |
.num_args = 0, | |
.flags = 0, | |
}, | |
php.zend_function_entry{ | |
.fname = null, | |
.handler = null, | |
.arg_info = null, | |
.num_args = 0, | |
.flags = 0, | |
}, | |
}; |