/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id: header 297205 2010-03-30 21:09:07Z johannes $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_testclass.h"
/* If you declare any globals in php_testclass.h uncomment this:*/
ZEND_DECLARE_MODULE_GLOBALS(testclass)
/**/
/* True global resources - no need for thread safety here */
static int le_testclass;
/////////////////////////////////////////////////////////////////////////////////////
/*
typedef unsigned int zend_object_handle;
typedef struct _zend_object_handlers zend_object_handlers;
typedef struct _zend_object_value {
zend_object_handle handle;
zend_object_handlers *handlers;
} zend_object_value;
*/
//struct _zend_object_handlers {
// /* general object functions */
// zend_object_add_ref_t add_ref;
// zend_object_del_ref_t del_ref;
// zend_object_clone_obj_t clone_obj;
// /* individual object functions */
// zend_object_read_property_t read_property;
// zend_object_write_property_t write_property;
// zend_object_read_dimension_t read_dimension;
// zend_object_write_dimension_t write_dimension;
// zend_object_get_property_ptr_ptr_t get_property_ptr_ptr;
// zend_object_get_t get;
// zend_object_set_t set;
// zend_object_has_property_t has_property;
// zend_object_unset_property_t unset_property;
// zend_object_has_dimension_t has_dimension;
// zend_object_unset_dimension_t unset_dimension;
// zend_object_get_properties_t get_properties;
// zend_object_get_method_t get_method;
// zend_object_call_method_t call_method;
// zend_object_get_constructor_t get_constructor;
// zend_object_get_class_entry_t get_class_entry;
// zend_object_get_class_name_t get_class_name;
// zend_object_compare_t compare_objects;
// zend_object_cast_t cast_object;
// zend_object_count_elements_t count_elements;
// zend_object_get_debug_info_t get_debug_info;
// zend_object_get_closure_t get_closure;
//};
/*************************************************************************/
zend_class_entry * car_ce;
zend_object_handlers car_object_handlers;
//汽车结构体
struct Car
{
int maxGear;
int currentGear;
int speed;
};
//汽车对象
struct car_object
{
zend_object std;
struct Car * car;
};
//销毁对象函数,相当析构函数
void car_object_dtor(void * object,zend_object_handle handle TSRMLS_DC)
{
struct car_object * intern = (struct car_object *)object;
zend_object_std_dtor( &(intern->std) TSRMLS_CC);
if( intern->car )
{
efree( intern->car ); //释放空间
}
efree( intern ); //释放空间
}
//创建对象,执行部分初始化
zend_object_value car_object_new(zend_class_entry * ce TSRMLS_DC)
{
zend_object_value retval;
struct car_object * intern;
intern = ecalloc(sizeof(struct car_object), 1);
zend_object_std_init( &(intern->std), ce TSRMLS_CC);
zend_hash_copy( intern->std.properties, &ce->default_properties,
(copy_ctor_func_t)zval_add_ref, NULL,
sizeof(zval *) );
//保存实例,并注册销毁回调函数
retval.handle = zend_objects_store_put( intern, NULL,car_object_dtor, NULL TSRMLS_CC);
retval.handlers = &car_object_handlers;
return retval;
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/* {{{ testclass_functions[]
*普通函数入口地址,由架构生成
* Every user visible function must have an entry in testclass_functions[].
*/
const zend_function_entry testclass_functions[] = {
PHP_FE(testclass_test, NULL)
PHP_FE(testclass_newclass, NULL)
PHP_FE(testclass_newcar,NULL)
PHP_FE(testclass_getclassbyparam,NULL)
{NULL, NULL, NULL} /* Must be the last line in testclass_functions[] */
};
/* }}} */
/*PHP_FE(testclass_test, NULL)*/
/*PHP_FE(confirm_testclass_compiled, NULL)*/ /* For testing, remove later. */
/*---------------------------------------------------------------------------*/
/*自定义类的函数入口地址,自己额外定义,也可以直接放在普通函数入口地址处声明*/
const zend_function_entry testclass_classfunctions[] = {
PHP_ME(Car, __construct, NULL, ZEND_ACC_CTOR | ZEND_ACC_PUBLIC)
PHP_ME(Car,__destruct,NULL,ZEND_ACC_DTOR | ZEND_ACC_PUBLIC)
PHP_ME(Car, __clone, NULL,ZEND_ACC_PUBLIC|ZEND_ACC_CLONE)
PHP_ME(Car, __toString, NULL,ZEND_ACC_PUBLIC)
PHP_ME(Car, shift, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, accelerate, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, brake, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, getCurrentSpeed, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, getCerrentGear, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/*---------------------------------------------------------------------------*/
/* {{{ testclass_module_entry
*/
zend_module_entry testclass_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"testclass",
testclass_functions,
PHP_MINIT(testclass),
PHP_MSHUTDOWN(testclass),
PHP_RINIT(testclass), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(testclass), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(testclass),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_TESTCLASS
ZEND_GET_MODULE(testclass)
#endif
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("testclass.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_testclass_globals, testclass_globals)
STD_PHP_INI_ENTRY("testclass.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_testclass_globals, testclass_globals)
PHP_INI_END()
/**/
/* }}} */
/* {{{ php_testclass_init_globals
*/
/* Uncomment this function if you have INI entries*/
static void php_testclass_init_globals(zend_testclass_globals *testclass_globals)
{
testclass_globals->global_value = 0;
testclass_globals->global_strin
评论0