Home Backend Development PHP Tutorial PHP extension development notes (1) Create array attributes of classes

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

Aug 08, 2016 am 09:23 AM
declare property zend

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(&amp;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-&gt;<span><span>type</span> &amp; <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>-&gt;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.

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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Notice: Trying to get property of non-object - Solution PHP Notice: Trying to get property of non-object - Solution Aug 17, 2023 am 09:27 AM

PHP Notice: Trying to get property of non-object - Solution

What are the usages of oracle declare What are the usages of oracle declare Sep 15, 2023 pm 01:12 PM

What are the usages of oracle declare

Solution to PHP Notice: Undefined property: Solution to PHP Notice: Undefined property: Jun 22, 2023 pm 02:48 PM

Solution to PHP Notice: Undefined property:

TypeError: Cannot read property 'XXX' of null in Vue, what should I do? TypeError: Cannot read property 'XXX' of null in Vue, what should I do? Nov 25, 2023 pm 01:21 PM

TypeError: Cannot read property 'XXX' of null in Vue, what should I do?

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (Access Control List) for permission control in Zend Framework

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

PHP Implementation Framework: Zend Framework Getting Started Tutorial

TypeError: Cannot read property '$XXX' of undefined in Vue, what are the solutions? TypeError: Cannot read property '$XXX' of undefined in Vue, what are the solutions? Nov 25, 2023 am 10:00 AM

TypeError: Cannot read property '$XXX' of undefined in Vue, what are the solutions?

TypeError: Cannot read property 'XXX' of undefined encountered in Vue project, how to deal with it? TypeError: Cannot read property 'XXX' of undefined encountered in Vue project, how to deal with it? Nov 25, 2023 pm 12:29 PM

TypeError: Cannot read property 'XXX' of undefined encountered in Vue project, how to deal with it?

See all articles