Home Backend Development PHP Tutorial Autoload tool for php objects extended by zend api_PHP tutorial

Autoload tool for php objects extended by zend api_PHP tutorial

Jul 21, 2016 pm 03:30 PM
api autoload php spl zend Function object tool Expand of

Similar to the autoload function of spl, bloader is an autoload tool for PHP objects, but it is simpler and more efficient, and the configuration is more flexible.

bloader provides a commonly used autoload function ld, and two auxiliary functions, ld_new( Instantiation) and ld_unset (destroy object).

#1 bloader will automatically search for the .class.php file in the current file or current directory, as well as the path defined by the '_MODULES' constant , the instantiated class returns the object.
#2 You can directly use ld('class name') to operate the object (see Example 1-1)
#3 bloader will automatically register a variable with the class name in the current scope The variable '$classname' (see Example 1-2)
#4 Using the ld function in bloader to access objects is globally valid (see Example 1-3)
#5 Use ld_new to instantiate multiple different object without registering variables (see Example 1-4)
#6 Use ld_unset to unregister an instantiated object (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 code The code is as follows:

///define('_MODULES',dirname( __FILE__ ).'/class'); ///Optional configuration, search for class files 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 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 global attributes*/
}
}
?>

Example 1-2
Copy code The code is as follows:

...
ld('users');
//The $users variable is automatically registered
$users->method();
....
?>

Example 1-3
Copy code The code is as follows:

ld('users');
printt(); //Print object
...
function printt()
{
var_dump(ld('users'));
}
?>

Example 1-4
Copy code The code is as follows:

$users_1=ld_new('users');
$users_2=ld_new('users');
.. .
?>

Example 1-5
Copy code The code is as follows:

ld('users');
unset_users();
...
function unset_users()
{
ld_unset('users');
}
?>

Provide the main code for bidding

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", 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;
}
...

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/323138.htmlTechArticle类似spl的autoload功能,bloader为php对象的autoload工具,但相比较起来更简单高效,配置也更灵活. bloader提供一个常用的autoload函数ld,以及两个辅助函...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

How to configure apscheduler timing task as a service on macOS? How to configure apscheduler timing task as a service on macOS? Apr 01, 2025 pm 06:09 PM

Configure the apscheduler timing task as a service on macOS platform, if you want to configure the apscheduler timing task as a service, similar to ngin...

Can Python parameter annotations use strings? Can Python parameter annotations use strings? Apr 01, 2025 pm 08:39 PM

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

In LangChain, how do I use AgentExecutor to replace the disabled initialize_agent function? In LangChain, how do I use AgentExecutor to replace the disabled initialize_agent function? Apr 01, 2025 pm 04:18 PM

How to replace the disabled initialize_agent function in LangChain? In the LangChain library, initialize_agent...

In the ChatGPT era, how can the technical Q&A community respond to challenges? In the ChatGPT era, how can the technical Q&A community respond to challenges? Apr 01, 2025 pm 11:51 PM

The technical Q&A community in the ChatGPT era: SegmentFault’s response strategy StackOverflow...

Where to download Python .whl files under Windows? Where to download Python .whl files under Windows? Apr 01, 2025 pm 08:18 PM

Python binary library (.whl) download method explores the difficulties many Python developers encounter when installing certain libraries on Windows systems. A common solution...

How to use Python and OCR technology to try to crack complex verification codes? How to use Python and OCR technology to try to crack complex verification codes? Apr 01, 2025 pm 10:18 PM

Exploration of cracking verification codes using Python In daily network interactions, verification codes are a common security mechanism to prevent malicious manipulation of automated programs...

See all articles