如何在我自己的php扩展中使用PDO支持?

时间:2012-12-20 06:22:04

标签: c pdo php-extension

我想在我的php扩展中使用PDO MySQL(使用c语言,而不是php中的简单pdo包装类),但我不知道这个案例,任何人都可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:4)

我的问题是由谷歌解决的,按照本指南可能会解决您的问题,它可以在其他zend类中使用。

注意:需要如何在c 中创建php扩展的基本概念。

1.你需要修补原点zend_call_method,因为原点zend_call_method 仅支持少于3个参数,而PDO的构造函数使用1-4个args。

  

PDO :: __ construct()(string $ dsn [,string $ username [,string $ password [,array $ driver_options]]])

在php源目录中查找“zend_interfaces.c”(可能在“php-5.xx-src \ Zend \ zend_interfaces.c”中),定位到“zend_call_method”部分,将其复制粘贴到您的代码中并执行这样:

/* {{{ zend_call_method
 Only returns the returned zval if retval_ptr != NULL */
ZEND_API zval* zend_call_method_n_params(
  zval **object_pp, zend_class_entry *obj_ce,
  zend_function **fn_proxy, const char *function_name,
  int function_name_len, zval **retval_ptr_ptr,
  int param_count, zval *** params TSRMLS_DC)
// ↑↑↑new function definition
// only change "zval* arg1, zval* arg2" to "zval *** params"
// make a new function name "zend_call_method_n_params"
{
        ...

  //zval **params[2];            ← delete it!
  //params[0] = &arg1;           ← delete it!
  //params[1] = &arg2;           ← delete it! done.

        ...
}
/* }}} */

注意:您可能需要一些宏:

#define call_method(obj, obj_ce, fn_proxy, function_name, retval, param_count, params) \
    zend_call_method_n_params(obj, obj_ce, fn_proxy, function_name, sizeof(function_name)-1, retval, param_count, params TSRMLS_CC)

#define call_function(function_name, retval, param_count, params) \
    zend_call_method(NULL, NULL, NULL, function_name, retval, param_count, params)

#define call_construct(obj, obj_ce, param_count, params) \
    call_method(obj, obj_ce, &obj_ce->constructor, obj_ce->constructor->common.function_name, NULL, param_count, params)

2.hello world!

设置代码,我们需要一个属性“pdo”

PHP_MINIT_FUNCTION(yourextension)
{
    ...
    zend_declare_property_null(you_class_entry, ZEND_STRL("pdo"),
                               ZEND_ACC_PUBLIC TSRMLS_CC);
    ...
}
PHP_METHOD(foo, bar)
{
    char * dsn = "mysql:dbname=yourdb;host=127.0.0.1;port=3306";
    char * usr = "username";
    char * pwd = "password";
    zval * zdsn = NULL;
    zval * zusr = NULL;
    zval * zpwd = NULL;

    zval ** params[3];
    zend_class_entry * pdo_class_entry;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
        RETURN_FALSE;
    }

    // get class entry for PDO
    pdo_class_entry = zend_fetch_class(ZEND_STRL("PDO"),
                                   ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

    // initial PDO instance
    MAKE_STD_ZVAL(pdo_inst);
    object_init_ex(pdo_inst, pdo_class_entry);

    // arg1: dns string
    ALLOC_INIT_ZVAL(zdns);
    ZVAL_STRINGL(zdsn, dsn, sizeof(dsn)-1, 1);

    // arg2: username
    ALLOC_INIT_ZVAL(zusr);
    ZVAL_STRINGL(zusr, usr, sizeof(usr)-1, 1);

    // arg3: password
    ALLOC_INIT_ZVAL(zpwd);
    ZVAL_STRINGL(zpwd, pwd, sizeof(pwd)-1, 1);

    // put them in params array
    params[0] = &zdsn;
    params[1] = &zusr;
    params[2] = &zpwd;

    // call PDO::__construct(zdns, zusr, zpwd)
    zend_call_method_n_params(&pdo_inst,
        pdo_class_entry,
        &pdo_class_entry->constructor,
        ZEND_STRL(pdo_class_entry->constructor->common.function_name),
        NULL, 3,    // 3 params
        params TSRMLS_CC);
    // or simply use macro instead()
    //call_construct(&pdo_inst, pdo_class_entry,3,params);
    if (pdo_inst && Z_TYPE_P(pdo_inst) == IS_OBJECT) {
        zend_update_property(your_class_entry, getThis(),
                             ZEND_STRL("pdo") , pdo_inst TSRMLS_CC);
        zend_printf("Success.\n");
    } else {
        zend_printf("Failure.\n");
    }
}

在php中可以像这样调用:

$foo = new foo();
$foo->bar(); // connect by PDO and update $foo->pdo
// you can use pdo property now
var_dump($foo->pdo->query("select * from table")->fetchAll());

工作正常。干杯!