Detailed PHP5 Object

Source: Internet
Author: User

PHP5 objects

It is not fair to compare the PhP5 object to its predecessor PhP4 object, although the API functions used by the PHP5 object are built by following the PHP4 API. If you have read the 10th chapter "PhP4 Object", you will be somewhat familiar with the content of this chapter. Before you begin this chapter, you can rename extensions to Sample3 and clean up extra code, just as you started in chapter 10th, leaving only the expanded skeleton code.

Evolutionary history

There are two key components in the PHP5 object variable. The first is the identification of a numeric value that is very similar to the numeric resource ID described in Chapter 9th, "Resource data types," and acts as a key to find an instance of an object in the corresponding table. The elements in this instance table contain references to zend_class_entry and the internal property sheet.

The second element is the handle table of the object variable, which allows you to customize how the Zend engine handles the instance. You will see this handle table later in this chapter.

Zend_class_entry

A class entry is an internal representation of a class that you define in user space. As you can see in the previous chapter, this structure is initialized by calling Init_class_entry (), and the parameter is the class name and its function table. The Zend_register_internal_class () registration is then used in the Minit phase.

Zend_class_entry *php_sample3_sc_entry;  
#define PHP_SAMPLE3_SC_NAME "Sample3_secondclass"  
static function_entry php_sample3_sc_functions[] = {  
    {NULL , NULL, NULL}  
};  
      
Php_minit_function (Sample3)  
{  
    zend_class_entry ce;  
    Init_class_entry (CE, php_sample3_sc_name,  
                            php_sample3_sc_functions);  
    Php_sample3_sc_entry =  
                Zend_register_internal_class (&ce tsrmls_cc);  
    return SUCCESS;  
}

Method

If you've read the previous chapter, you might think, "So far it looks pretty much the same?" So far, you're right. Now we begin to define some object methods. You will begin to see some very definite and popular differences.

Php_method (Sample3_secondclass, HelloWorld)  
{  
    php_printf ("Hello world\n");  
}

The Php_method () macro was introduced in Zend Engine 2, which is the encapsulation of the php_function () macro, combining the class name with the method name, without having to manually define the method name as in PhP4. By using this macro, the namespace parsing specification of your code and other maintainer's code in the extension is consistent.

Defined

Defining a method's implementation, like other functions, simply connects it to the class's function table. In addition to the Php_method () macros for implementation, there are new macros that can be used in the definition of the function list.

Php_me (classname, MethodName, Arg_info, flags)

Php_me () adds a classname parameter and a flags parameter at the end (used to provide public, protected, private, static, and so on, compared to the PHP_FE () macros described in the 5th chapter "Your first extension". As well as abstract and some other options). For example, to define the HelloWorld method, you can define the following:

Php_me (Sample3_secondclass,helloworld,null,zend_acc_public)

Php_malias (classname, name, alias, Arg_info, Flags)

Like the Php_falias () macro, this macro allows you to provide a new name specified by name for the method described by the alias parameter (in the same class). For example, to copy your HelloWorld method, you can define the following

Php_malias (Sample3_secondclass, Sayhi, HelloWorld,  
                            NULL, Zend_acc_public)

Php_abstract_me (classname, MethodName, Arg_info)

Abstract methods in an inner class are much like abstract methods of user space. In the parent class it is just a placeholder, expecting its subclass to provide a true implementation. You will use this macro in the interface section, which is a special class_entry.

Php_me_mapping (methodname, functionname, Arg_info)

The last method defines a macro that exposes both OOP and non-OOP interfaces (for example, Mysqli has both a procedural mysqli_query () and an object-oriented mysqlite::query () that uses the same implementation.) . Assuming you already have a procedural function, such as the 5th Chapter Sample_hello_world (), you can use the macro to attach it as a class method (note that the mapping method is always public, not static, not final):

Php_me_mapping (Hello, Sample_hello_world, NULL)

So far, the method definition you see uses the zend_acc_public as its flags argument. In fact, this value can be a combination of bit fields for any value of the following two tables, and it can also use a bit field Operation combination with a special method tag that is described in the "Special methods" section later in this chapter.

For example, because the Sample3_secondclass::helloworld () method you defined earlier does not require an object instance, you can modify its definition from a simple zend_acc_public to a Zend_acc_public | Zend_acc_static, so that the engine knows not to provide (the example).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.