Original: Http://my.oschina.net/mickelfeng/blog/122519?p=1
Suppose we want to implement a class person with the PHP extension, which has a private member variable $_name and two public instance methods GetName () and SetName (), which can be expressed in PHP code as follows:
?
| 123456789101112 |
<?php class Person{ private $_name; public function getName() { return $this -> _name; } public function setName($name) { $this -> _name = $name; } } |
1. Declaration method: Also use the first article used in the example, first in the header file Php_fetion_echo.h added method declaration.
Php_method (person, __construct); Php_method (person, __destruct); Php_method (person, getName); Php_method (person, setName);
The preceding extension uses the Php_function macro when declaring a function, whereas when implementing a class extension we use the Php_method macro, the first parameter specifies the class name, and the second parameter specifies the method name.
2. Method implementation: In the FETION_ECHO.C file implementation of these methods, constructors and destructors just output some text.
?
| 1234567891011121314151617181920212223242526272829 |
PHP_METHOD(Person, __construct) { php_printf("__construct called.");}PHP_METHOD(Person, __destruct) { php_printf("__destruct called.<br/>");}PHP_METHOD(Person, getName) { zval *self, *name; self = getThis(); name = zend_read_property(Z_OBJCE_P(self), self, ZEND_STRL("_name"), 0 TSRMLS_CC); RETURN_STRING(Z_STRVAL_P(name), 0);}PHP_METHOD(Person, setName) { char *arg = NULL; int arg_len; zval *value, *self; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { WRONG_PARAM_COUNT; } self = getThis(); MAKE_STD_ZVAL(value); ZVAL_STRINGL(value, arg, arg_len, 0); SEPARATE_ZVAL_TO_MAKE_IS_REF(&value); zend_update_property(Z_OBJCE_P(self), self, ZEND_STRL("_name"), value TSRMLS_CC); RETURN_TRUE;} |
Do some explaining to the above code:
A. Get the parameter information of the method, still use the Zend_parse_parameters function, as we have described before;
B. Get the This pointer (as opposed to PHP code, still using the zval structure representation in PHP extensions) using the Getthis () function;
C. Use the Make_std_zval macro to request and initialize a zval structure, in the PHP extension, all data types are actually represented by the zval structure, in this series of articles I will write a separate article to introduce Zval.
D. Get property values use the Zend_read_property () function to update the property values with the Zend_update_property () function.
3. Initialize the class: in the extended initialization function, register and initialize the class.
Zend_class_entry *person_ce; Php_minit_function (Fetion_echo) { "person", fetion_echo_functions); Person_ce = ZEND_REGISTER_INTERNAL_CLASS_EX (&person, NULL, null TSRMLS_CC); Zend_declare_property_null (Person_ce, Zend_strl (return SUCCESS;}
Initializes the class with the Init_class_entry macro, the second parameter specifies the class name, and the third parameter is the function table.
4. Register to function: Declare the parameters of the method and register it in the function table.
Zend_begin_arg_info (arg_person_setname, 0) const Zend_function_entry fetion_echo_functions[] = { PHP_ME (Person, __construct, NULL, zend_acc_public| Zend_acc_ctor) Php_me (person, __destruct, NULL, zend_acc_public| Zend_acc_dtor) Php_me (person, getName, NULL, Zend_acc_public) Php_me (person, setName, arg_person_ SetName, Zend_acc_public)/* Must be the last line in fetion_echo_functions[] */};
The declaration of a class method parameter is consistent with the way in which we previously declared the function parameter, using the PHP_ME macro when registering the class method into the function table, rather than the Php_fe macro that was previously used.
Zend_acc_public: The access modifier for the specified method
Zend_acc_ctor: Specifies that the method is a constructor
Zend_acc_dtor: Specifies that this method is a destructor
5. Run the test: after compiling and installing the extension, write a simple test script:
New person (); $person->setname (echo $person->getname (). <br/> ';
After running, you can see the following output, indicating that the extension is working properly:
__construct Called.mickelfeng__destruct called.
Develop a PHP class with extensions