Development of PHP Extensions development of our own interface class

Source: Internet
Author: User
Tags export class git client using git
This article to share the content is the development of PHP expansion of our own interface class, has a certain reference value, the need for friends can refer to

PHP Extension is one of the skills that advanced PHP programmers must understand, how can a beginner PHP extension developer develop a mature extension into the advanced areas of PHP development? This series of development tutorials will take you from entry to the advanced stage.
This tutorial series was developed under Linux (recommended for CentOS), PHP is 5.6, and assumes you have a certain Linux experience and a C/s base.
There are problems need to communicate with friends Please add QQ Technology Group 32550793 and I communicate.

The previous chapter demonstrates how to export common functions in a PHP extension, and this chapter describes how to export classes in extensions. Enables PHP to directly access the C + + classes in the extension in the script.

First, how to export C + + classes in PHP extensions

The following is an extended skeleton code developed using Php-cpp, which compiles to export a demo C + + class.

The source of the project is on GitHub, which can be downloaded using git client or open URL.

# git clone https://github.com/elvisszhang/phpcpp_counter.git

Now our class name is Counter, and the syntax of the extended inside registration class is like this

Php::class<counter> Counter ("Counter");

The counter class has a function called increment, which tells the extension to allow the PHP script to access the function using the following syntax.

Counter.method<&counter::increment> ("increment");

Main.cpp C + + source code is as follows.

Export classes for #include <phpcpp.h> #include <time.h>//extensions counterclass counter:public php::base{private:int _value =    0;public:counter () = default;    Virtual ~counter () = default;    The ordinary member function of the class Php::value increment () {return ++_value;}    Php::value Decrement () {return--_value;}    Php::value Value () const {return _value;} Static member function of Class Php::value gettime () {return time ();}};/ /tell the compiler that Get_module is a pure C function extern "C" {//get_module is an extended entry function Phpcpp_export void *get_module () {static php::extens                Ion Myextension ("Counter", "1.0.0");                Initialize the Export class php::class<counter> Counter ("Counter");        The accessible normal function counter.method<&counter::increment> ("increment") of the registered export class;        Counter.method<&counter::d ecrement> ("decrement");                Counter.method<&counter::value> ("value");        Registers an accessible static function counter.method<&counter::gettime> ("gettime") for the exported class;     Register an export class and use rvalue referencing to optimize resource usage   Myextension.add (Std::move (counter));    Returns the extended object pointer return myextension; }}

The PHP test code that corresponds to the above example is as follows.

<?php$counter = new Counter;echo ' Result of increment () = '. $counter->increment (). Php_eol;echo ' result of increment () = '. $counter->increment (). Php_eol;echo ' result of decrement () = '. $counter->decrement (). Php_eol;echo ' result of value () = '. $counter->value (). Php_eol;echo ' result of gettime () = '. Counter::gettime (). Php_eol;? >

The output information after the above PHP code is run is as follows.

Result of increment () = 1result of increment () = 2result of decrement () = 1result of value () = 1result of gettime () = 1523 363778

II. styles supported by common functions of the extended class

The functions of the extended class must be written according to certain specifications, and the names and types of the return values and parameters are specified. Otherwise, it cannot be understood by PHP scripts.

The most common is the following 4 kinds of function styles, followed by the previous chapter of the normal function of the same style, the return value and the use of parameters is exactly the same, so no more say.

Signatures of supported regular methodsvoid        yourclass::example1 (); void        Yourclass::example2 (Php:: Parameters &params); Php::value  yourclass::example3 (); Php::value  yourclass::example4 (Php::P arameters &params);

The other function if the modifier is const. There are also 4 variations of the following styles.

void        yourclass::example5 () const;void        yourclass::example6 (Php::P arameters &params) const; Php::value  Yourclass::example7 () const; Php::value  Yourclass::example8 (Php::P arameters &params) const;

Iii. Reference Documents

Php-cpp Help: Classes-and-objects


PHP Extension is one of the skills that advanced PHP programmers must understand, how can a beginner PHP extension developer develop a mature extension into the advanced areas of PHP development? This series of development tutorials will take you from entry to the advanced stage.
This tutorial series was developed under Linux (recommended for CentOS), PHP is 5.6, and assumes you have a certain Linux experience and a C/s base.
There are problems need to communicate with friends Please add QQ Technology Group 32550793 and I communicate.


The previous chapter demonstrates how to export common functions in a PHP extension, and this chapter describes how to export classes in extensions. Enables PHP to directly access the C + + classes in the extension in the script.

Related Article

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.