日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php扩展 zval_copy_ctor,zend api扩展的php对象的autoload工具

發(fā)布時間:2025/4/16 php 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php扩展 zval_copy_ctor,zend api扩展的php对象的autoload工具 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

類似spl的autoload功能,bloader為php對象的autoload工具,但相比較起來更簡單高效,配置也更靈活.

bloader提供一個常用的autoload函數(shù)ld,以及兩個輔助函數(shù),ld_new(實例化)和ld_unset(銷毀對象).

#1 bloader會自動搜索當前文件 或 當前目錄下的.class.php文件,以及通過'_MODULES'常量定義的路徑,實例化類返回對象.

#2 可直接使用ld('類名')操作對象(見實例 1-1)

#3 bloader會在當前作用域自動注冊一個以類名為變量名的變量'$類名'(見實例 1-2)

#4 bloader中使用ld函數(shù)訪問對象是全局范圍有效 (見實例 1-3)

#5 使用ld_new實例化多個不同的對象,而不注冊變量 (見實例 1-4)

#6 使用ld_unset注銷已經(jīng)實例化的對象 (見實例 1-5)

下載地址:http://code.google.com/p/bloader/downloads/detail?name=bloader.tar.gz

安裝:

phpize

./configure --with-php-config=php-config --enable-bloader

make && make install

實例 1-1

///define('_MODULES',dirname( __FILE__ ).'/class'); ///可選配置,在指定目錄下查找類文件,以便于實例化

ld('c1',array('1','2'))->a1="a1"; ///參數(shù)2為構(gòu)造函數(shù)的參數(shù)

ld('c1')->a2='a2';

ld('c1')->printt();

/**

show:

c1 Object

(

[a1] => a1

[a2] => a2

[a3] => Array

(

[0] => 1

[1] => 2

)

)

*/

?>

/**

example:

./class/c1.class.php:

*/

class c1

{

public $a1=123;

public $a2='abc';

public $a3=100;

public function __construct($ls)

{

$this->a3=$ls;

}

public function printt()

{

print_r(ld('c1')); /**使用了全局特性*/

}

}

?>

實例 1-2

...

ld('users');

//自動注冊了$users變量

$users->method();

....

?>

實例 1-3

ld('users');

printt(); //打印對象

...

function printt()

{

var_dump(ld('users'));

}

?>

實例 1-4

$users_1=ld_new('users');

$users_2=ld_new('users');

...

?>

實例 1-5

ld('users');

unset_users();

...

function unset_users()

{

ld_unset('users');

}

?>

奉上主要代碼供拍磚

...

PHP_FUNCTION(ld)

{

char *obj_name;

int slen;

zval **var,*para = NULL;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &obj_name,&slen,?) != SUCCESS)

{

zend_error(E_ERROR, "parameters failed.");

}

else

{

zval_dtor(return_value);

if(zend_hash_find(&EG(symbol_table),obj_name,slen+1,(void **) &var)!=SUCCESS)

{

ld_autoload_path(obj_name TSRMLS_DC);

*return_value = *ld_new_class(obj_name,slen,para,1);

}

else

{

*return_value = **var;

}

zval_copy_ctor(return_value);

}

}

PHP_FUNCTION(ld_new)

{

char *obj_name;

int slen;

zval *para = NULL;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &obj_name,&slen,?) != SUCCESS)

{

zend_error(E_ERROR, "parameters failed.");

}

else

{

zval_dtor(return_value);

ld_autoload_path(obj_name TSRMLS_DC);

*return_value = *ld_new_class(obj_name,slen,para,0);

zval_copy_ctor(return_value);

}

}

PHP_FUNCTION(ld_unset)

{

char *obj_name;

int slen;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &obj_name,&slen) != SUCCESS)

{

zend_error(E_ERROR, "parameters failed.");

}

else

{

zend_hash_del(&EG(symbol_table),obj_name,slen+1);

RETURN_TRUE;

}

}

/* }}} */

static zval *ld_new_class(char *obj_name,int slen,zval *para,int is_set)

{

zval *obj;

zend_class_entry **class_entry;

zend_function *constructor;

MAKE_STD_ZVAL(obj);

if(zend_lookup_class(obj_name, slen, &class_entry TSRMLS_CC)==SUCCESS)

{

object_init_ex(obj, *class_entry);

constructor = Z_OBJ_HT_P(obj)->get_constructor(obj TSRMLS_CC);

if (constructor != NULL)

{

int is_arg = (para == NULL) ? 0 : 1;

zend_call_method(&obj, *class_entry,&constructor, "__construct", 11, NULL, is_arg, para, NULL TSRMLS_CC);

}

if(is_set==1) ZEND_SET_SYMBOL(&EG(symbol_table),obj_name, obj);

}

else

{

ZVAL_FALSE(obj);

}

return obj;

}

static int ld_autoload_path(char *class_name TSRMLS_DC)

{

char *ext_name = ".class.php";

char *file_path;

zval const_root;

int path_len = spprintf(&file_path, 0, "%s%s",class_name,ext_name);

if(ld_autoload_file(file_path,path_len TSRMLS_DC)==SUCCESS) return SUCCESS;

if(zend_get_constant("_MODULES",8,&const_root TSRMLS_CC))

//if(zend_get_constant_ex("_MODULES",8,const_root,NULL, 0 TSRMLS_CC)) //ZEND_FETCH_CLASS_SILENT

{

if(Z_TYPE(const_root) == IS_STRING)

{

char *root_file_path;

int root_path_len = spprintf(&root_file_path, 0, "%s/%s", Z_STRVAL(const_root),file_path);

return ld_autoload_file(root_file_path,root_path_len TSRMLS_DC);

}

}

return FAILURE;

}

static int ld_autoload_file(char *file_path,int file_path_len TSRMLS_DC) /* {{{ */

{

zend_file_handle file_handle;

if (php_stream_open_for_zend_ex(file_path, &file_handle, ENFORCE_SAFE_MODE|USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC) == SUCCESS)

{

zend_op_array *new_op_array;

unsigned int dummy = 1;

if (!file_handle.opened_path) file_handle.opened_path = estrndup(file_path, file_path_len);

if (zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL)==SUCCESS)

{

new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE TSRMLS_CC);

zend_destroy_file_handle(&file_handle TSRMLS_CC);

}

else

{

new_op_array = NULL;

zend_file_handle_dtor(&file_handle TSRMLS_CC);

}

if (new_op_array)

{

zval *result = NULL;

EG(return_value_ptr_ptr) = &result;

EG(active_op_array) = new_op_array;

if (!EG(active_symbol_table)) zend_rebuild_symbol_table(TSRMLS_C);

zend_execute(new_op_array TSRMLS_CC);

destroy_op_array(new_op_array TSRMLS_CC);

efree(new_op_array);

if (!EG(exception)) if (EG(return_value_ptr_ptr))

zval_ptr_dtor(EG(return_value_ptr_ptr));

}

return SUCCESS;

}

return FAILURE;

}

...

總結(jié)

以上是生活随笔為你收集整理的php扩展 zval_copy_ctor,zend api扩展的php对象的autoload工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。