PHP-X Series Tutorial: Defining Classes and Objects in Extensions

  php, php-x, zend-api

Following the previous article, this article began to implement the definition of classes and object operations within the extension. Based onPHP-XPackages provided, usingC++Language can easily implement aPHPThe class of can also be found in theC++Create anyPHPObject, calling object methods, reading and writing object properties.

0x00 defines PHP classes

BottomZend APIThere 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 toExtensionTheonStartIn the callback.

  • new ClassTo create a newPHPClass, parameter is the name of the class
  • callClassObject’saddMethodTo add methods, wherePHPX_ME(CppClass, test2)HerePHPX_MEIs a macro, expansion is the name of the method and function pointer, the second parameter is the modifier of the method, can beSTATICPROTECTEDABSTRACTWait. You can use bitwise or operator settings to set multiple modifiers, such asSTATIC | PUBLIC
  • callClassObject’saddPropertyAdd attribute
  • callClassObject’saddConstantAdd 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 theaddMethodBefore.

AndPHPX_FUNCTIONThe function is the same.PHPX_METHODThere are alsoargsAndretvalTwo parameters for processingPHPParameters and return values of method calls. the difference isPHPX_METHODMore than one._thisObject, equivalent toPHPIn the code$this.Note that if it is a static method_thisFornull

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

UseconstantFunction 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-XYou can also createPHPObjects, andPHPInteractive 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());

newObjectThe function is equivalent toPHPThenewSyntax, 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.

callexecFunctions 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 istrueTo verify that the connection was established correctly.

if (ret1.toBool()) {
 //Connection Successful
 } else {
 //Connection Failed
 }

InPHP-XNot only can you createPHPExtend built-in classes and createPHPClass defined by the code.

include("/data/webroot/Test.class.php");
 Object testObj = newObject("Test", 123, 456, "hello");