In PHP, object orientation is closely related to class. Let's look at how class interfaces, inheritance, Private, protected, and public are implemented in the kernel.
I. Create a class
Zend_class_entry is a struct defined in the kernel and is the basic structure type of classes and objects in PHP.
How can I define myclass classes in extensions and instantiate them in PHP?
PHP looks like this:
<?php$obj = new myclass();?>
In the kernel:
Zend_class_entry * myclass; // defines the global pointer variable and points to the myclass class. Static zend_function_entry myclass_method [] = {// method in myclass {null, null, null} php_minit_function (extension_name) {zend_class_entry class; init_class_entry (class, "myclass", myclass_method ); myclass = zend_register_internal_class (& Class tsrmls_cc); Return success ;}
Zend_class_entry of a class is often used, so we usually save it in a global variable for programs in other places of the extension.
Corresponding to the implementation of the above myclass class, the code in PHP:
<?phpclass myclass{}?>
This class has no attributes or methods.
Ii. Create class attributes
Before introducing the attributes of a class, first understand the representation of public, protected, and private in the class in the kernel.
#define ZEND_ACC_STATIC 0x01 /* Static */#define ZEND_ACC_PUBLIC 0x100 /* Public */#define ZEND_ACC_PROTECTED 0x200 /* Protected */#define ZEND_ACC_PRIVATE 0x400 /* Private */
You can use the zend_declare_property _ * series functions provided by the kernel to define the attributes of a class. Three information is required at the same time:
Attribute name, default value, and access permission.
<?phpclass myclass{private my_var="this is property";}?>
In the kernel:
zend_class_entry *myclass;static zend_function_entry myclass_method[] = {{NULL, NULL, NULL}}PHP_MINIT_FUNCTION(extension_name){zend_class_entry class;INIT_CLASS_ENTRY(class, "myclass", myclass_method);myclass = zend_register_internal_class(&class TSRMLS_CC);zend_declare_property_string(myclass, "my_var", strlen("my_var"), "this is property", ZEND_ACC_PRIVATE TSRMLS_CC);}
// The macro zend_api int zend_declare_property Defining class attributes is provided in the kernel (zend_class_entry * ce, char * Name, int name_length, zval * property, int access_type tsrmls_dc ); zend_api int limit (zend_class_entry * ce, const char * Name, int name_length, zval * property, int access_type, char * doc_comment, int limit tsrmls_dc); zend_api int limit (zend_class_entry * ce, char * Name, int name_length, int access_type tsrmls_dc); zend_api int zend_declare_property_bool (zend_class_entry * ce, char * Name, int name_length, long value, int access_type tsrmls_dc ); zend_api int limit (zend_class_entry * ce, char * Name, int name_length, long value, int access_type tsrmls_dc); zend_api int limit (zend_class_entry * ce, char * Name, int name_length, double value, int access_type tsrmls_dc); zend_api int partition (zend_class_entry * ce, char * Name, int name_length, char * value, int access_type tsrmls_dc); zend_api int partition (zend_class_entry * CE, char * Name, int name_length, char * value, int value_len, int access_type tsrmls_dc );
3. Create methods in the class
# Define zend_acc_static 0x01/* Static Method */# define zend_acc_abstract 0x02/* abstract Method */# define zend_acc_final 0x04/* final state method */# define zend_acc_implemented _ 0x08/* fn_flags */# define zend_acc_interactive 0x10/* fn_flags */# define zend_acc_public 0x100/* Public */# define zend_acc_protected 0x200/* protected * /# define zend_acc_private 0x400/* Private */# define zend_acc_changed 0x800/* fn_flags, optional */# define zend_acc_ctor 0x2000/* _ construct */# define zend_acc_dtor 0X4000/* _ destruct */# define zend_acc_clone 0x8000/* _ clone */# define defaults 0x10000/* fn_flags */# define zend_acc_shadow 0x20000/* fn_flags */# define defaults 0x40000/* fn_flags */# define zend_acc_closure 0 x 100000/* fn_flags */# define zend_acc_call_via_handler 0x200000/* fn_flags */
Before implementing methods in the class, you should first understand zend_function_entry.
In the new extension, there is a zend_function_entry. The kernel connects the functions in PHP language with the functions written in C language. zend_function_entry is equivalent to a bridge.
Class, it is also used to connect the class methods in PHP with the method written in C language.
<?phpclass myclass{public function __construct(){echo "init myclass!";}public function hello(){echo "hello";}}?>
In the kernel:
zend_class_entry *myclass;PHP_METHOD(myclass, __construct){php_printf("init myclass!");}PHP_METHOD(myclass, hello){php_printf("hello");}static zend_function_entry myclass_method[] = {ZEND_ME(myclass, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR);ZEND_ME(myclass, hello, NULL, ZEND_ACC_PUBLIC);{NULL, NULL, NULL}}PHP_MINIT_FUNCTION(extension_name){zend_class_entry class;INIT_CLASS_ENTRY(class, "myclass", myclass_method);myclass = zend_register_internal_class(&class TSRMLS_CC);return SUCCESS;}
4. Create constants in the class
Use the APIS provided by the kernel.
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value TSRMLS_DC);ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length TSRMLS_DC);ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, long value TSRMLS_DC);ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value TSRMLS_DC);ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value TSRMLS_DC);ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length TSRMLS_DC);ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value TSRMLS_DC);
<?phpclass myclass{const class_name = "myclass";}?>
In the kernel:
zend_class_entry *myclass;static zend_function_entry myclass_method[] = {{NULL, NULL, NULL}}PHP_MINIT_FUNCTION(extension_name){zend_class_entry class;INIT_CLASS_ENTRY(class, "myclass", myclass_method);myclass = zend_register_internal_class(&class TSRMLS_CC);zend_declare_class_constant_stringl(myclass, "class_name", strlen("class_name"), "myclass", strlen("myclass") TSRMLS_CC);}