The AutoLoad function, like SPL, bloader the AutoLoad tool for PHP objects, but is simpler and more efficient and more flexible to configure.
Bloader provides a common autoload function, LD, and two auxiliary functions, ld_new (instantiation) and Ld_unset (destroying objects).
#1 Bloader will automatically search for the current file or the current directory <类名> The. class.php file, along with the path defined by the ' _modules ' constant, instantiates the class to return the object.
#2 can manipulate objects directly using LD (' Class name ') (see example 1-1)
#3 Bloader will automatically register a variable ' $ class name ' with the class name as the variable name in the current scope (see example 1-2)
Accessing an object using the LD function #4 Bloader is globally valid (see Example 1-3)
#5 instantiate multiple different objects with ld_new without registering the variables (see example 1-4)
#6 unregister an already instantiated object using Ld_unset (see example 1-5)
Download Address: http://code.google.com/p/bloader/downloads/detail?name=bloader.tar.gz
Installation:
Phpize
./configure--with-php-config=php-config--enable-bloader
Make && make install
Example 1-1
Copy CodeThe code is as follows:
Define (' _modules ', DirName (__file__). ' /class '); Optional configuration to find the class file under the specified directory for easy instantiation
LD (' C1 ', Array (' 1 ', ' 2 '))->a1= "A1"; Parameter 2 is the parameter of the constructor
LD (' C1 ')->a2= ' A2 ';
LD (' C1 ')->printt ();
/**
Show
C1 Object
(
[a1] = A1
[A2] = A2
[a3] = = Array
(
[0] = 1
[1] = 2
)
)
*/
?>
Copy CodeThe code is as follows:
/**
Example
./class/c1.class.php:
*/
Class C1
{
Public $a 1=123;
Public $a 2= ' abc ';
Public $a 3=100;
Public function __construct ($LS)
{
$this->a3= $ls;
}
Public Function Printt ()
{
Print_r (LD (' C1 ')); /** uses the global feature */
}
}
?>
Example 1-2
Copy CodeThe code is as follows:
...
LD (' users ');
$users variables are automatically registered
$users->method ();
....
?>
Example 1-3
Copy CodeThe code is as follows:
LD (' users ');
Printt (); Print objects
...
function Printt ()
{
Var_dump (LD (' users '));
}
?>
Example 1-4
Copy CodeThe code is as follows:
$users _1=ld_new (' users ');
$users _2=ld_new (' users ');
...
?>
Example 1-5
Copy CodeThe code is as follows:
LD (' users ');
Unset_users ();
...
function Unset_users ()
{
Ld_unset (' users ');
}
?>
The main code for the brick
Copy CodeThe code is as follows:
...
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", one, 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;
}
...
http://www.bkjia.com/PHPjc/323138.html www.bkjia.com true http://www.bkjia.com/PHPjc/323138.html techarticle The autoload function, like SPL, bloader the AutoLoad tool for PHP objects, but is simpler, more efficient, and more flexible to configure. Bloader provides a common autoload function ld, and two auxiliary letters ...