How to Create a PHP Extension Object: Step‑by‑Step Guide

This tutorial walks through creating a PHP extension object by defining a zend_class_entry, naming the class, registering it, declaring properties, and implementing methods, complete with full example code and a PHP usage snippet.

21CTO
21CTO
21CTO
How to Create a PHP Extension Object: Step‑by‑Step Guide

Step 1: Register the class entry

Define a zend_class_entry variable to act as the prototype.

zend_class_entry ce;

Step 2: Set the class name

Use INIT_CLASS_ENTRY to give the class a name and associate its method table.

INIT_CLASS_ENTRY(ce, "children", children_methods);

Step 3: Register the class

Call zend_register_internal_class to obtain a pointer that will be used for later operations.

zend_class_entry *children_ce;
children_ce = zend_register_internal_class(&ce);

Step 4: Declare a property

Declare a public property called memory using the zend_declare_property_null family.

zend_declare_property_null(children_ce, "memory", sizeof("memory")-1, ZEND_ACC_PUBLIC);

Step 5: Define a method

Describe the method signature with ZEND_BEGIN_ARG_INFO_EX and register it in the function entry table.

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_ME(children, learn, arginfo_children_learn, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

Step 6: Implement the method

Parse the incoming string, store it in the memory property, and return.

PHP_METHOD(children, learn)
{
    char *love;
    size_t love_len;

    if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "s", &love, &love_len) == FAILURE) {
        return;
    }

    zend_update_property_string(children_ce, getThis(), "memory", sizeof("memory")-1, love);
}

Full extension code

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);
    return SUCCESS;
}

Using the class from PHP

<?php
$children = new children();
var_dump($children->memory);
$children->learn("love");
var_dump($children->memory);
?>

The output shows NULL for the initial property value and then string(4) "love" after calling learn.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPObject CreationExtension
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.