修改(创建PHP扩展)Makefile以包含C++类和代码


Modifying a (PHP Extension creating) Makefile to include C++ classes and code

这个问题最适合Linux C++(和C)MAKE FILES专家,但也需要了解PHP扩展以及如何在Linux上创建它们。

上下文

我正在C++中编写一个与TCP套接字相关的多线程底层PHP扩展,目标是Linux(CentOS 6.5 64位)。正如您可能知道的,PHP扩展本质上"扩展了用户空间PHP代码的功能"。

我选择用标准C++编写核心功能和逻辑,使用标准C++生成文件(由Eclipse CDT生成),它创建了一个标准的Linux可执行二进制文件,称为DC

现在,我需要能够从我正在创建的PHP扩展(及其导出的函数)中调用一些类(和函数)。

其他详细信息

PHP扩展的编写可能有点棘手,因为您必须从源代码构建PHP,然后使用"phpize"、"./configure"等,以及config.m4文件。这个对phpize AUTO的调用生成了一个带有大量make文件设置的MAKEFILE,这让我不知所措

需要您帮助的问题

我现在需要的是将我的标准C++类(代码)"转移"到PHP扩展项目目录中,并让它们与PHP扩展进行编译、构建和链接-正如你所知,是作为共享对象(.SO)(在Linux上)和.DLL(在Windows上)创建的。

我遇到的这个问题本质上是关于**修改由对"phpize"的调用自动生成的主MAKEFILE,以便能够编译和链接额外的C++代码**(非C代码)。基本上,我需要将肉(保存在我的C++项目中)添加到我已经开发并正在运行的基本框架PHP扩展中。

任何有这方面知识的人-请随时帮助我,并衷心感谢你。。,提前。:)

以下是我们的操作方法:

我们在库中编译C++代码(正如您已经提到的),然后链接它。此外,我们告诉phpize激活C++编译器。

PHP_ARG_ENABLE(myextension, whether to enable myextension support,
[ --enable-myextension   Enable myextension support])
if test "$PHP_MYEXTENSION" = "yes"; then
  AC_DEFINE(HAVE_MYEXTENSION, 1, [Whether you have myExtension])
  PHP_REQUIRE_CXX()
  PHP_SUBST(MYEXTENSION_SHARED_LIBADD)
  PHP_ADD_INCLUDE(../../../include)
  PHP_ADD_INCLUDE(/usr/include)
  PHP_ADD_INCLUDE(/opt/local/include)
  PHP_ADD_LIBRARY(stdc++, 1, MYEXTENSION_SHARED_LIBADD)
  PHP_ADD_LIBRARY_WITH_PATH(boost_thread-mt, /opt/local/lib, MYEXTENSION_SHARED_LIBADD)
  PHP_ADD_LIBRARY_WITH_PATH(boost_system-mt, /opt/local/lib, MYEXTENSION_SHARED_LIBADD)
  PHP_ADD_LIBRARY_WITH_PATH(customlib, /path/to/custom/lib, MYEXTENSION_SHARED_LIBADD)
  AC_DEFINE(_ADDITIONAL_DEFINES_,1,[_ADDITIONAL_DEFINES_])
  PHP_NEW_EXTENSION(MYEXTENSION, src/php.cpp, $ext_shared)
fi

在php.cpp中,我们必须确保某些东西在C:中

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#ifdef WIN32
/* PHP Extension headers */    
/* include zend win32 config first */    
#include "zend_config.w32.h"
#endif
extern "C" {
#include "php.h"
#include "php_ini.h"
#ifdef ZTS
#include "TSRM.h"
#endif
#include "zend_exceptions.h"
}

我们还必须包装我们的C++类:

zend_object_handlers myExtension_object_handlers;
struct myextension_object {
    zend_object std;
    YourNS::Extension::myExtension *extension;
};
void myExtension_free_storage(void *object TSRMLS_DC)
{
    myextension_object *obj = (myextension_object *)object;
    delete obj->myExtension; 
    zend_hash_destroy(obj->std.properties);
    FREE_HASHTABLE(obj->std.properties);
    efree(obj);
}
zend_object_value myExtension_create_handler(zend_class_entry *type TSRMLS_DC)
{
    zval *tmp;
    zend_object_value retval;
    myextension_object *obj = (myextension_object *)emalloc(sizeof(myextension_object ));
    memset(obj, 0, sizeof(myextension_object ));
    obj->std.ce = type;
    ALLOC_HASHTABLE(obj->std.properties);
    zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
    #if PHP_VERSION_ID < 50399 
    zend_hash_copy(obj->std.properties, &type->default_properties,
                   (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
    #else
    object_properties_init(&obj->std, type);
    #endif
    retval.handle = zend_objects_store_put(obj, NULL,
                                           myExtension_free_storage, NULL TSRMLS_CC);
    retval.handlers = &myExtension_object_handlers;
    return retval;
}

以后你可以很方便地使用它:

PHP_METHOD(myExtension, __construct)
{
    YourNS::Extension::myExtension *myExt = NULL;
    zval *object = getThis();
    //if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &maxGear) == FAILURE) {
    //    RETURN_NULL();
    //}
    myExt = new YourNS::Extension::myExtension();
    myextension_object *obj = (myextension_object *)zend_object_store_get_object(object TSRMLS_CC);
    obj->extension = myExt;
}
PHP_METHOD(myExtension, connect)
{
    char* host, *user, *secret, *db, *dbtype, *baseschema;
    int host_len, user_len, secret_len, db_len, dbtype_len, baseschema_len;
    long port;
    YourNS::Extension::myExtension *ext;
    myextension_object *obj = (myextension_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
    ext= obj->extension;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slsssss", &host, &host_len, &port, &user, &user_len, &secret, &secret_len, &db, &db_len, &dbtype, &dbtype_len, &baseschema, &baseschema_len) == FAILURE) {
        RETURN_NULL();
    }
    if ( ext != NULL) {
        try {
            ext->connect(host, port, user, secret, db, dbtype, baseschema);
        }
        catch (const YourNS::Extension::RuntimeException& e) {
            char* msg;
            msg = estrdup(e.getMessage().c_str());
            zend_throw_exception(zend_exception_get_default(TSRMLS_C), msg, 0 TSRMLS_CC);
            efree(msg);
        }
    }
}