PHP Extension Development Notes (1) Creating an array property of a class

Source: Internet
Author: User
Initializing a class is easy, such as the following code
Myclass_proterty_* This correlation is the Define macro string

Zend_class_entry *myclass_ce;zend_function_entry myclass_methods[] = {Php_fe_end};    Php_minit_function (myext) {zend_class_entry CE; Init_class_entry (CE,"MyClass", myclass_methods);    Myclass_ce = Zend_register_internal_class (&ce tsrmls_cc); Zend_Declare_class_constant_string (Myclass_ce, Zend_strl (myclass_proterty_name_version), php_slim_version); Zend_Declare_property_null (Myclass_ce, Zend_strl (Myclass_proterty_name_container), Zend_acc_public TSRMLS_CC); Zend_Declare_property_null (Myclass_ce, Zend_strl (Myclass_proterty_name_apps), zend_acc_static|    Zend_acc_protected tsrmls_cc); Zend_Declare_property_null (Myclass_ce, Zend_strl (myclass_proterty_name_name), zend_acc_protected TSRMLS_CC); Zend_Declare_property_null (Myclass_ce, Zend_strl (myclass_proterty_name_error), zend_acc_protected TSRMLS_CC); Zend_Declare_property_null (Myclass_ce, Zend_strl (myclass_proterty_name_notfound), zend_acc_protected TSRMLS_CC); Zend_Declare_property_null (Myclass_ce, Zend_strl (myclass_proterty_name_middleware), zend_acc_protected TSRMLS_CC);returnSUCCESS;}

The above code is a few simple properties.
When you want to initialize an array's properties to the MyClass class, it fails, as opposed to the PHP code as follows

classMyClass {    public $myArray = array();}/* 对应的C代码 */zval *myArray;MAKE_STD_ZVAL(myArray);array_init(myArray);zend_declare_property(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_MYCLASS), myArray, ZEND_ACC_PUBLICTSRMLS_CC);

The above C code mutation did not find any problem, in the implementation of new MyClass () when the problem occurred, the error is as follows:

Internal zval's can'tor resources

Look at the Zend source code as follows:

if (ce->type & ZEND_INTERNAL_CLASS) {     switch(Z_TYPE_P(property)) {         caseIS_ARRAY:         caseIS_CONSTANT_ARRAY:         caseIS_OBJECT:         caseIS_RESOURCE:             zend_error(E_CORE_ERROR, "Internalzval'scan'tbearrays, objectsorresources");             break;         default:             break;     } }

When we call Zend_register_internal_class, Myclass_ce is initialized to Zend_internal_class, and at this point the zend_declare_ The MyArray parameter of the property is of type Is_array, so this error is generated.

Why does it make such a mistake?

The results I found below are: http://grokbase.com/t/php/php-internals/07a4b14xvb/ Php-dev-how-declare-protected-array-property-at-internal-class-properly This is the result of the 2007, I am using the php5.4 version of this, and temporarily still have this problem, the text also gives a disguised way to implement the array properties, by the constructor in the implementation.

PHP_METHOD(myclass, __construct) {    *apps*pThis;    pThis = getThis();    MAKE_STD_ZVAL(apps);    array_init(apps);    add_property_zval_ex(pThis, ZEND_STRL(SLIM_SLIM_PROTERTY_NAME_APPS), apps);}

This way of implementing the corresponding PHP code

classMyClass {function__construct() {$thisarray();      }}

The above describes the PHP extension development Note (1) to create an array of classes of properties, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.