PHP extension development notes (1) Create array attributes of classes

WBOY
Release: 2016-08-08 09:23:26
Original
1502 people have browsed it

It is very easy to initialize a class, such as the code below
MYCLASS_PROTERTY_* This is related to the macro string of define

<code>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, <span>"MyClass"</span>, myclass_methods);
    myclass_ce = zend_register_internal_class(&ce TSRMLS_CC);

    zend_<span>declare</span>_class_constant_string(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_VERSION), PHP_SLIM_VERSION);
    zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_CONTAINER), ZEND_ACC_PUBLIC TSRMLS_CC);
    zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_APPS), ZEND_ACC_STATIC|ZEND_ACC_PROTECTED TSRMLS_CC);
    zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_NAME), ZEND_ACC_PROTECTED TSRMLS_CC);
    zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_ERROR), ZEND_ACC_PROTECTED TSRMLS_CC);
    zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_NOTFOUND), ZEND_ACC_PROTECTED TSRMLS_CC);
    zend_<span>declare</span>_property_null(myclass_ce, ZEND_STRL(MYCLASS_PROTERTY_NAME_MIDDLEWARE), ZEND_ACC_PROTECTED TSRMLS_CC);

    <span>return</span> SUCCESS;
}</code>
Copy after login

The above codes are all simple properties.
When trying to initialize the attributes of an array for the class myclass, it failed. The code relative to PHP is as follows

<code><span><span>class</span><span>MyClass</span> {
    public $myArray = array<span>()</span>;
}


/* 对应的<span>C</span>代码 */

zval *myArray;
<span>MAKE_STD_ZVAL</span><span>(<span>myArray</span>)</span>;
array_init<span>(<span>myArray</span>)</span>;

zend_declare_property<span>(<span>myclass_ce</span>, <span>ZEND_STRL(MYCLASS_PROTERTY_NAME_MYCLASS)</span>, <span>myArray</span>, <span>ZEND_ACC_PUBLIC</span><span>TSRMLS_CC</span>)</span>;</span></code>
Copy after login

No problem was found when the above C code was mutated. When new MyClass() was executed, There is a problem, the error is as follows:

<code>Internal zval<span>'s</span> can<span>'t</span> be arrays, objects <span>or</span> resources</code>
Copy after login

Look at the source code of zend as follows:

<code><span>if</span> (ce-><span><span>type</span> & <span>ZEND_INTERNAL_CLASS</span>) <span>{
     <span>switch</span>(<span>Z_TYPE_P(property)</span>) {
         <span>case</span><span>IS_ARRAY</span>:
         <span>case</span><span>IS_CONSTANT_ARRAY</span>:
         <span>case</span><span>IS_OBJECT</span>:
         <span>case</span><span>IS_RESOURCE</span>:
             <span>zend_error</span>(<span>E_CORE_ERROR</span>, "<span>Internal</span><span>zval's</span><span>can't</span><span>be</span><span>arrays</span>, <span>objects</span><span>or</span><span>resources</span>");
             <span>break</span>;
         <span>default</span>:
             <span>break</span>;
     }</span></span>
 }</code>
Copy after login

When we call zend_register_internal_class, myclass_ce has been initialized to ZEND_INTERNAL_CLASS, and the myArray parameter of zend_declare_property at this time is of type IS_ARRAY. So this error occurred.

Why does such an error occur?

The result I got after searching is: http://grokbase.com/t/php/php-internals/07a4b14xvb/php-dev-how-declare-protected-array-property-at-internal-class-properly this It is the result of 2007. I am using the PHP5.4 version, and I still have this problem for the time being. The article also gives a method to implement array attributes in disguise, by implementing it in the constructor.

<code>PHP_METHOD(myclass, __construct) {
    zval <span>*apps</span>, <span>*pThis</span>;
    pThis = getThis();
    MAKE_STD_ZVAL(apps);
    array_init(apps);
    add_property_zval_ex(pThis, ZEND_STRL(SLIM_SLIM_PROTERTY_NAME_APPS), apps);
}</code>
Copy after login

The corresponding php code for this implementation

<code><span><span>class</span><span>MyClass</span> {</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>$this</span>->app = <span>array</span>();  
    }
}</code>
Copy after login

The above introduces the PHP extension development notes (1) Array attributes of the created class, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!