Following the previous article, this article began to implement the definition of classes and object operations within the extension. Based onPHP-X
Packages provided, usingC++
Language can easily implement aPHP
The class of can also be found in theC++
Create anyPHP
Object, calling object methods, reading and writing object properties.
0x00 defines PHP classes
BottomZend API
There is a limit, the definition function must be before module initialization, and the definition class must be after module initialization. Therefore, to define a class, you need toExtension
TheonStart
In the callback.
-
new Class
To create a newPHP
Class, parameter is the name of the class - call
Class
Object’saddMethod
To add methods, wherePHPX_ME(CppClass, test2)
HerePHPX_ME
Is a macro, expansion is the name of the method and function pointer, the second parameter is the modifier of the method, can beSTATIC
、PROTECTED
、ABSTRACT
Wait. You can use bitwise or operator settings to set multiple modifiers, such asSTATIC | PUBLIC
- call
Class
Object’saddProperty
Add attribute - call
Class
Object’saddConstant
Add class constant
PHPX_EXTENSION()
{
Extension *ext = new Extension("test", "0.0.1");
ext->onStart = [ext]
{
Class *c = new Class("CppClass");
//Register Construction Method
c->addMethod(PHPX_ME(CppClass, __construct), CONSTRUCT);
//Common method
c->addMethod(PHPX_ME(CppClass, test1));
//Common method
c->addMethod(PHPX_ME(CppClass, test2), STATIC);
//Add default attribute
c->addProperty("age", 30);
//Add Constant
c->addConstant("VERSION", "1.9.0");
//Register Class
ext->registerClass(c);
};
return ext;
}
0x01 Implement Class Methods
Methods are different from attributes and constants. No additional operations are required after adding attributes and constants. Class method, you need to write the actual logic code.
UsePHPX_METHOD (class name, method name)
Just. Note that the code implemented by the method must be in theaddMethod
Before.
AndPHPX_FUNCTION
The function is the same.PHPX_METHOD
There are alsoargs
Andretval
Two parameters for processingPHP
Parameters and return values of method calls. the difference isPHPX_METHOD
More than one._this
Object, equivalent toPHP
In the code$this
.Note that if it is a static method_this
Fornull
PHPX_METHOD(CppClass, __construct)
{
echo("%s _construct\n", _this.getClassName().c_str());
Array array;
array.append(1234);
_this.set("name", array);
}
PHPX_METHOD(CppClass, test1)
{
//Read global variables
Variant server = global("_SERVER");
var_dump(server);
if (server.isArray())
{
Variant shell = Array(server)["SHELL"];
var_dump(shell);
}
auto name = _this.get("name");
var_dump(name);
}
0x02 Read and Write Properties
- call
_this.get(property_name)
Read the value of an object property - call
_this.set(property_name, value)
Sets the value of an object property
0x03 Read Class Constant
Useconstant
Function to get a class constant.
auto value = constant("CppClass::VERSION");
var_dump(value); //Output 1.9.0
0x04 compilation
make install
0x05 operation
$o = new CppClass();
$o->test1();
0x06 Create PHP Object
InPHP-X
You can also createPHP
Objects, andPHP
Interactive operation.
Object redis = newObject("redis");
//Connect Redis
auto ret1 = redis.exec("connect", "127.0.0.1", 6379);
//Get Key
auto ret2 = redis.exec("get", "key");
//Print string
echo ("value=%s\n", ret2.toCString());
newObject
The function is equivalent toPHP
Thenew
Syntax, the first parameter is the class name and becomes longer from the second parameter, which is the parameter of the construction method, such asnewObject("test", 123, 456, "hello")
This is equivalent tonew test(123, 456, "hello")
. The object created is in theC++
The type in isObject
.
callexec
Functions can execute methods of objects, such asredis.exec("connect", "127.0.0.1", 6379)
, is equivalent to is$redis->connect("127.0.0.1", 6379)
. Returns the result of a method call after execution.
It can be judged whether the result istrue
To verify that the connection was established correctly.
if (ret1.toBool()) {
//Connection Successful
} else {
//Connection Failed
}
InPHP-X
Not only can you createPHP
Extend built-in classes and createPHP
Class defined by the code.
include("/data/webroot/Test.class.php");
Object testObj = newObject("Test", 123, 456, "hello");