dsa99349 2017-07-20 02:34
浏览 31

如何在php-extension中将php对象保存到hashtable?

How to save a php object to hashtable and how to get a saved php obejct from hashtable in php-extension?

Here is my code:

//object_map has been init and alloc memory
//return_value is the return var of PHP_FUNCTION

zend_class_entry **class_ce,
object_init_ex(return_value, *class_ce); //create object
ioc_add_object_to_hash(name, return_value ); //save object

//find in the hashtable
if( zend_hash_find( object_map, name, sizeof(name), (void*)&return_value ) == SUCCESS ){

    php_printf("return_value:%p, %p,type:%d
", return_value,&return_value, Z_TYPE_P(return_value));
    return 0;
}

//definition of ioc_add_object_to_hash
int ioc_add_object_to_hash( const char *name, zval *obj )
{
    if( !obj ){
        return -1;
    }
    if( !object_map ){
        return -1;
    }
    if( zend_hash_update( object_map, name, sizeof(name), obj, sizeof(*obj), NULL) == SUCCESS ){
        return 0;
    }
    return -1;
}

PHP code:

   $filelist = array(
       "Foo" => realpath(__DIR__)."/Foo.php",
       "Bar" => realpath(__DIR__)."/Bar.php",
   );

   ioc::init( $filelist );


  var_dump(get_included_files());

  Bar::halo();

  $foo = ioc::make("Foo");

  echo $foo->get();

  $foo2 = ioc::make("Foo"); var_dump($foo2);

the $foo2 is NULL after second call ioc::make("Foo");

all my code is push to https://github.com/longmon/php-ioc.git.

Thank you!

  • 写回答

1条回答 默认 最新

  • doudichu1358 2017-07-20 07:34
    关注

    Hei,I solved myself! My purpose is get the object from hashtable, the problem is i did wrong way to init return_value, so i got the NULL.The right way would be like below:

    if( !object_map ){
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "object_map had not been alloced");
        return -1;
    }
    zval **obj;
    if( zend_hash_find( object_map, name, sizeof(name), (void **)&obj ) == SUCCESS ){
        Z_TYPE_P(return_value)   = IS_OBJECT;
        Z_OBJVAL_P(return_value) = Z_OBJVAL_PP(obj);
        //zval_ptr_dtor(obj);
        return 0;
    }
    return -1;
    

    The key line is :

    Z_TYPE_P(return_value)   = IS_OBJECT;
    Z_OBJVAL_P(return_value) = Z_OBJVAL_PP(obj);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序