#include "phpx.h"
#include <iostream>
using namespace php;
using namespace std;
PHPX_FUNCTION(cpp_ext_test)
{ for (int i = 0; i < args.count(); i++) { cout << args[i].type() << endl; } retval = 1234; }PHPX_FUNCTION(cpp_ext_test2)
{ for (int i = 0; i < args.count(); i++) { cout << args[i].type() << endl; } auto v1 = args[0]; Array arr(v1); arr.set(1, "efg");retval = arr;
//php::echo("argc=%d\n", args.count()); //php::error(E_WARNING, "extension warning."); }PHPX_METHOD(myClass, test)
{ cout << "myClass::test" << endl; retval = 1234.56; }PHPX_METHOD(myClass, pget)
{ String *str = _this.oGet<String>("resource", "ResourceString"); cout << "ResourceString: " << str->length() << endl; }PHPX_METHOD(myClass, pset)
{ _this.oSet("resource", "ResourceString", new String("hello world")); }void string_dtor(zend_resource *res)
{ String *s = static_cast<String *>(res->ptr); delete s; } static zend_op_array* (*old_compile_file)(zend_file_handle*, int TSRMLS_DC); zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type) { cout<<"compile"; cout <<file_handle->filename; return old_compile_file(file_handle, type TSRMLS_CC); } PHPX_EXTENSION() { Extension *extension = new Extension("cpp_ext", "0.0.1");extension->onStart = [extension]() noexcept
{ old_compile_file=zend_compile_file; zend_compile_file = persistent_compile_file; extension->registerConstant("CPP_EXT_VERSION", 1002); cout<<"in startup"; Class *c = new Class("myClass"); c->addMethod(PHPX_ME(myClass, test), STATIC); c->addMethod(PHPX_ME(myClass, pget)); c->addMethod(PHPX_ME(myClass, pset)); extension->registerClass(c);extension->registerResource("ResourceString", string_dtor);
};// extension->onShutdown = [extension]() noexcept
// { // cout << extension->name << "shutdown" << endl; // }; // // extension->onBeforeRequest = [extension]() noexcept // { // cout << extension->name << "beforeRequest" << endl; // }; // // extension->onAfterRequest = [extension]() noexcept // { // cout << extension->name << "afterRequest" << endl; // };extension->registerFunction(PHPX_FN(cpp_ext_test));
extension->registerFunction(PHPX_FN(cpp_ext_test2));extension->info(
{ "cpp_ext support", "enabled" }, { { "author", "Rango" }, { "version", extension->version }, { "date", "2017-05-22" }, });return extension;
}
这个是根据PHP-X中的cpp_ext这个demo改的。只实现了一个功能,就是拦截PHP编译时的opcode。
拦截opcode有什么作用?可以做加密扩展也可以做opcode反编译。
之前有用过PHPCPP,功能也不错,但是用到PHP内置API的时候不太方便,这个PHP-X确实挺方便,而且也挺小。
其他的话,两个框架还是比较相似的。
配置环境中可能会出现一个问题,就是无法加载libphpx.so,这个是因为这个库没有被加载到全局lib库后没有更新缓存,所以会提示这个错误。只需要执行下面命令即可
/sbin/ldconfig –v