白话 PHP7 扩展开发:创建对象
本篇文章主要将如何在扩展中创建一个对象。创建的对象的过程,其实和一个小孩出生,成长的过程有些类似。
第一步,办准生证
生孩子第一步,先办准生证。声明我要生孩子了。对象创建的时候,如何办准生证呢?只要定义一个zend_class_entry变量即可。代码如下:
zend_class_entry ce;
zend_class_entry 是啥?可以认为它使一个原型,定义了一些对象应该有哪些东西组成。具体代码可以查看./Zend/zend.h文件。
第二步,取名字
孩子怎么得有个名字,对象也一样。如何给对象取名字呢?代码如下:
INIT_CLASS_ENTRY(ce, "children", children_methods);
其中,children就是我们给对象取的名字。那children_methods是啥?它是这个小孩应该具备的能力(对象所拥有的方法)。
第三步,上户口
孩子出生了,名字有了,下面就得上户口了。上户口的过程就是登记入册的过程。代码如下:
zend_class_entry *children_ce;children_ce = zend_register_internal_class(&ce);
zend_register_internal_class方法会返回一个zend_class_entry指针。以后我们对这个对象的操作,可以使用这个指针。
培养(定义属性和方法)
孩子已经上户口了。接下来,我们就是认真的培养他。培养成对社会有用的人。那么如何培养呢?主要从两方面入手。第一方面是教授知识(定义属性),另一方面是培养其行为能力(定义方法)。定义属性,我们使用zend_declare_property*系列方法。代码示例如下:
zend_declare_property_null(children_ce, "memory", sizeof("memory") - 1, ZEND_ACC_PUBLIC);
上面的代码我们就声明了一个名称为memory的属性,并且设置访问类型为 public。
定义方法的过程更简单。还记得我们在上户口的时候,登记了children_methods。这个就是孩子行为的一个集合。这个行为集合如何产生的呢?代码如下:
ZEND_BEGIN_ARG_INFO_EX(arginfo_children_learn, 0, 0, 1) ZEND_ARG_INFO(0, love)ZEND_END_ARG_INFO()PHP_METHOD(children, learn);const zend_function_entry children_functions[] = {PHP_FE(learn, arginfo_children_learn){NULL, NULL, NULL}}
ZEND_BEGIN_ARG_INFO_EX的最后一个参数1,是传递的参数的个数。ZEND_ARG_INFO的第一个参数0,表示是否传引用方式传递。一般默认为0即可。
完整代码
至此,我们已经定义了一个对象,拥有属性memory和方法learn。完整的代码如下:
zend_class_entry *children_ce;ZEND_BEGIN_ARG_INFO_EX(arginfo_children_learn, 0, 0, 1) ZEND_ARG_INFO(0, love)ZEND_END_ARG_INFO()const zend_function_entry children_functions[] = {PHP_ME(children, learn, arginfo_children_learn, ZEND_ACC_PUBLIC){NULL, NULL, NULL}}PHP_MINIT_FUNCTION(children) {zend_class_entry ce;INIT_CLASS_ENTRY(ce, "children", children_methods);children_ce = zend_register_internal_class(&ce);zend_declare_property_null(children_ce, "memory", sizeof("memory") - 1, ZEND_ACC_PUBLIC);}PHP_METHOD(children, learn){char *love;size_t love_len;if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "s",&content, &content_len) == FAILURE) { return;}zend_update_property_string(children_ce, getThis(), "memory", sizeof("memory") - 1, love);}
PHP调用代码
memory);$children->learn("love");var_dump($children->memory);?>
输出内容如下:
NULLstring(4) "love"
代码下载
完整的扩展源代码下载

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











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
