Zendapi extends the php object's autoload tool. Similar to spl's autoload function, bloader is the php object's autoload tool, but it is simpler, more efficient, and more flexible in configuration. bloader provides a common autoload function ld and two auxiliary functions similar to spl's autoload function. bloader is the php object's autoload tool, but it is simpler, more efficient, and more flexible in configuration.
Bloader provides a common autoload function ld and two auxiliary functions, ld_new (instantiation) and ld_unset (destroy object ).
#1 bloader will automatically search for the current file or <类名> The. class. php file, and the path defined by the '_ modules' constant, instantiate the class and return the object.
#2 you can directly use ld ('class name') to operate an object (see instance 1-1)
#3 bloader automatically registers a variable '$ Class name' with the class name in the current scope (see instance 1-2)
#4 use the ld function in the bloader to access objects within the global range (see instance 1-3)
#5 use ld_new to instantiate multiple different objects without registering variables (see instance 1-4)
#6 use ld_unset to deregister an instantiated object (see instance 1-5)
: Http://code.google.com/p/bloader/downloads/detail? Name1_bloader.tar.gz
Installation:
Phpize
./Configure -- with-php-config = php-config -- enable-bloader
Make & make install
Instance 1-1
The code is as follows:
/// Define ('_ les', dirname (_ FILE __). '/class'); // optional configuration. you can search for class files in the specified directory to facilitate 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
)
)
*/
?>
The code is as follows:
/**
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 ');/** use the global feature */
}
}
?>
Instance 1-2
The code is as follows:
...
Ld ('users ');
// The $ users variable is automatically registered.
$ Users-> method ();
....
?>
Instance 1-3
The code is as follows:
Ld ('users ');
Printt (); // Print the object
...
Function printt ()
{
Var_dump (ld ('users '));
}
?>
Instance 1-4
The code is as follows:
$ Users_1 = ld_new ('users ');
$ Users_2 = ld_new ('users ');
...
?>
Instance 1-5
The code is as follows:
Ld ('users ');
Unset_users ();
...
Function unset_users ()
{
Ld_unset ('users ');
}
?>
Provide the main code for brick making
The 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, parameters ,¶)! = 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, parameters ,¶)! = 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", 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;
}
...
Vertex. bloader provides a common autoload function ld and two auxiliary functions...