


PHP 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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Oracle declare usage includes variable declaration, constant declaration, cursor declaration and subroutine declaration. Detailed introduction: 1. Variable declaration, in PL/SQL block, you can use the DECLARE statement to declare variables; 2. Constants are unchangeable values declared in PL/SQL block; 3. Cursor declaration, used in PL/SQL The query result set is processed in the block; 4. Subroutine declaration. A subroutine is a reusable code block defined in a PL/SQL block.

PHPNotice: Tryingtogetpropertyofnon-object-Solution During the PHP development process, we may encounter a common error message: Tryingtogetpropertyofnon-object (trying to get the property of a non-object). This error is usually caused when we try to access a property (or call a method) on a variable that is not an object type. This article will introduce you to this

When writing code in PHP, we may encounter the error message "Notice: Undefinedproperty". This error means that we are accessing an undefined property, usually because the property has not been initialized in the code. So, how to solve this problem? Here are a few possible solutions: Initialize properties This is the simplest way to solve this problem. Explicitly initializing a property in code ensures that it is defined before use. For example: class

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

Vue is a popular JavaScript framework for building user interfaces. During the development process, we may encounter various errors and exceptions. One of the common errors is "TypeError:Cannotreadproperty'XXX'ofnull". In this article, we will explore the causes of this error and how to fix it. First, let’s understand the reason behind this error. When we try to access a property or method of an object, if the pair

TypeError:Cannotreadproperty'XXX'ofundefined in Vue, what should I do? For front-end developers who use Vue to develop, they may often encounter TypeError:Cannotreadproperty'XXX'ofundefined errors during the development process. This error usually occurs when trying to access an undefined property. exist

TypeError:Cannotreadproperty'$XXX'ofundefined in Vue, what are the solutions? In Vue development, errors such as TypeError:Cannotreadproperty'$XXX'ofundefined are often encountered. This error is usually caused by using undefined properties or methods in the Vue instance. When this error occurs, we need to
