Zend API extended PHP object AutoLoad tools _php Tips

Source: Internet
Author: User
Similar to SPL's autoload feature, Bloader is a autoload tool for PHP objects, but is simpler and more efficient and more flexible to configure.

Bloader provides a commonly used autoload function LD, as well as two auxiliary functions, ld_new (instantiation) and Ld_unset (object destruction).

#1 Bloader automatically searches the current file or the < class name >.class.php file under the current directory, as well as the path defined by the ' _modules ' constant, instantiating the class to return the object.
#2 can directly use the LD (' class name ') Operation object (see Example 1-1)
#3 Bloader automatically registers a variable ' $ class name ' with the class name as the variable name in the current scope (see example 1-2)
#4 Bloader using the LD function to access an object is global scope effective (see example 1-3)
#5 instantiate several different objects using ld_new without registering the variable (see instance 1-4)
#6 unregister an object that has already been instantiated 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

Instance 1-1
Copy Code code as follows:

<?php
Define (' _modules ', DirName (__file__). /class '); Optional configuration to find the class file in 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 Code code as follows:

<?php
/**
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 global characteristics */
}
}
?>

Instance 1-2
Copy Code code as follows:

<?php
...
LD (' users ');
$users variables are automatically registered
$users->method ();
....
?>

Instance 1-3
Copy Code code as follows:

<?php
LD (' users ');
Printt (); Print objects
...
function Printt ()
{
Var_dump (LD (' users '));
}
?>

Instance 1-4
Copy Code code as follows:

<?php
$users _1=ld_new (' users ');
$users _2=ld_new (' users ');
...
?>

Instance 1-5
Copy Code code as follows:

<?php
LD (' users ');
Unset_users ();
...
function Unset_users ()
{
Ld_unset (' users ');
}
?>

The main code for the brick

Copy Code code 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;
}
...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.