search
HomeBackend DevelopmentPHP TutorialPHP extension development notes (1) Create array attributes of classes

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>

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>

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>

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>

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>

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>

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
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
PHP Notice: Trying to get property of non-object - 解决方法PHP Notice: Trying to get property of non-object - 解决方法Aug 17, 2023 am 09:27 AM

PHPNotice:Tryingtogetpropertyofnon-object-解决方法在PHP开发过程中,我们可能会遇到一个常见的错误提示:Tryingtogetpropertyofnon-object(试图获取非对象的属性)。这个错误通常是由我们对一个非对象类型的变量尝试访问属性(或调用方法)时引起的。这篇文章将向你介绍这

oracle declare用法有哪些oracle declare用法有哪些Sep 15, 2023 pm 01:12 PM

oracle declare用法有变量声明、常量声明、游标声明和子程序声明。详细介绍:1、变量声明,在PL/SQL块中,可以使用DECLARE语句声明变量;2、常量是在PL/SQL块中声明的不可更改的值;3、游标声明,用于在PL/SQL块中处理查询结果集;4、子程序声明,子程序是在PL/SQL块中定义的可重用的代码块。

PHP Notice: Undefined property: 的解决方法PHP Notice: Undefined property: 的解决方法Jun 22, 2023 pm 02:48 PM

在使用PHP编写代码时,我们可能会遇到“Notice:Undefinedproperty”这个错误提示。这个错误提示意味着我们正在访问一个未定义的属性,通常是因为该属性在代码中尚未被初始化。那么,该如何解决这个问题呢?下面是几种可能的解决方法:初始化属性这是解决该问题的最简单方法。在代码中显式地初始化属性,可以确保它在使用前已经被定义。例如:class

Vue中的TypeError: Cannot read property 'XXX' of null,应该怎么办?Vue中的TypeError: Cannot read property 'XXX' of null,应该怎么办?Nov 25, 2023 pm 01:21 PM

Vue是一种流行的用于构建用户界面的JavaScript框架。在开发过程中,我们可能会遇到各种错误和异常。其中一个常见的错误是"TypeError:Cannotreadproperty'XXX'ofnull"。在本文中,我们将探讨这个错误的原因以及如何解决它。首先,让我们来了解一下这个错误的背后原因。当我们尝试访问一个对象的属性或方法时,如果该对

如何在Zend框架中使用ACL(Access Control List)进行权限控制如何在Zend框架中使用ACL(Access Control List)进行权限控制Jul 29, 2023 am 09:24 AM

如何在Zend框架中使用ACL(AccessControlList)进行权限控制导言:在一个Web应用程序中,权限控制是至关重要的一项功能。它可以确保用户只能访问其有权访问的页面和功能,并防止未经授权的访问。Zend框架提供了一种方便的方法来实现权限控制,即使用ACL(AccessControlList)组件。本文将介绍如何在Zend框架中使用ACL

PHP实现框架:Zend Framework入门教程PHP实现框架:Zend Framework入门教程Jun 19, 2023 am 08:09 AM

PHP实现框架:ZendFramework入门教程ZendFramework是PHP开发的一种开源网站框架,目前由ZendTechnologies维护,ZendFramework采用了MVC设计模式,提供了一系列可重用的代码库,服务于实现Web2.0应用程序和Web服务。ZendFramework深受PHP开发者的欢迎和推崇,拥有广泛

Vue中的TypeError: Cannot read property '$XXX' of undefined,解决方法有哪些?Vue中的TypeError: Cannot read property '$XXX' of undefined,解决方法有哪些?Nov 25, 2023 am 10:00 AM

Vue中的TypeError:Cannotreadproperty'$XXX'ofundefined,解决方法有哪些?在Vue开发中,经常会遇到TypeError:Cannotreadproperty'$XXX'ofundefined这样的错误。这种错误通常是因为在Vue实例中使用了未定义的属性或方法而引起的。出现这个错误时,我们需要

Vue项目中遇到的TypeError: Cannot read property 'XXX' of undefined,应该如何处理?Vue项目中遇到的TypeError: Cannot read property 'XXX' of undefined,应该如何处理?Nov 25, 2023 pm 12:29 PM

Vue项目中遇到的TypeError:Cannotreadproperty'XXX'ofundefined,应该如何处理?在Vue的开发过程中,我们经常会遇到TypeError:Cannotreadproperty'XXX'ofundefined这样的错误。这个错误通常是由于在代码中尝试访问一个未定义的属性而导致的。在这篇文章中,我将

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

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

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),