01
02
03
04
05
06
|
function
__autoload(
$classname
) {
require
(PATH_TO_CLASS.
'/'
.
$classname
.
'.class.php'
);
}
$obj
=
new
MyObject();
?>
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
ZEND_API int zend_lookup_class(char *name, int name_length, zend_class_entry ***ce TSRMLS_DC)
{
zval **args[1];
zval autoload_function;
zval class_name, *class_name_ptr = &class_name;
zval *retval_ptr;
int retval;
char *lc_name;
zval *exception;
char dummy = 1;
lc_name = do_alloca(name_length + 1);
/* 把类名转换成小写 */
zend_str_tolower_copy(lc_name, name, name_length);
/* 查找要进行new操作的类 */
if
(zend_hash_find(EG(class_table), lc_name, name_length+1, (void **) ce) == SUCCESS) {
/* 找到的话,返回SUCCESS */
free_alloca(lc_name);
return
SUCCESS;
}
if
(zend_is_compiling(TSRMLS_C)) {
free_alloca(lc_name);
return
FAILURE;
}
/* 找不到要进行new操作的类 */
if
(EG(in_autoload) == NULL) {
ALLOC_HASHTABLE(EG(in_autoload));
zend_hash_init(EG(in_autoload), 0, NULL, NULL, 0);
}
if
(zend_hash_add(EG(in_autoload), lc_name, name_length+1, (void**)&dummy, sizeof(char), NULL) == FAILURE) {
free_alloca(lc_name);
return
FAILURE;
}
/* 构建一个__autoload函数名变量 */
ZVAL_STRINGL(&autoload_function,
"__autoload"
, sizeof(
"__autoload"
)-1, 0);
/* 要进行new操作的类名 */
INIT_PZVAL(class_name_ptr);
ZVAL_STRINGL(class_name_ptr, name, name_length, 0);
args[0] = &class_name_ptr;
exception = EG(exception);
EG(exception) = NULL;
/* 调用用户自定义的__autoload函数 */
retval = call_user_function_ex(EG(function_table), NULL, &autoload_function, &retval_ptr, 1, args, 0, NULL TSRMLS_CC);
zend_hash_del(EG(in_autoload), lc_name, name_length+1);
if
(retval == FAILURE) {
EG(exception) = exception;
free_alloca(lc_name);
return
FAILURE;
}
if
(EG(exception)) {
free_alloca(lc_name);
zend_error(E_ERROR,
"__autoload(%s) threw an exception of type '%s'"
, name, Z_OBJCE_P(EG(exception))->name);
return
FAILURE;
}
EG(exception) = exception;
zval_ptr_dtor(&retval_ptr);
/* 再一次从类表中查找要进行new操作的类 */
retval = zend_hash_find(EG(class_table), lc_name, name_length + 1, (void **) ce);
free_alloca(lc_name);
return
retval;
}
|