PHP-CPP開發擴展(六)

PHP-CPP是一個用於開發PHP擴展的C++庫。本節講解在C++中PHP異常、變量、常量的實現相關知識。php

異常

PHP和C++都支持異常,而PHP-CPP庫這兩種語言之間的異常處理是徹底透明的。你在C++中拋出的異常會自動傳遞給PHP腳本,而且你的C++代碼能夠捕獲PHP腳本拋出的異常,就像它是普通的C++異常同樣。數組

遺憾的是,PHP-CPP目前僅支持PHP標準異常Exception,還不支持自定義異常。app

拋出異常

示例:函數

#include <phpcpp.h>

Php::Value myDiv(Php::Parameters &params)
{
    if (params[1] == 0) throw Php::Exception("Division by zero");

    return params[0] / params[1];
}

extern "C" {
    PHPCPP_EXPORT void *get_module() {
        static Php::Extension extension("helloworld", "1.0.0");
        extension.add<myDiv>("myDiv", {
            Php::ByVal("a", Php::Type::Numeric, true),
            Php::ByVal("b", Php::Type::Numeric, true)
        });
        return extension;
    }
}

測試:測試

echo myDiv(3,2);
echo myDiv(3,0);

捕獲異常

void myDivTest(){
    try{
        Php::call("myDiv", 3,2);
        Php::call("myDiv", 3,0);
    }catch(Php::Exception &e){
        Php::out << "Division by zero" << std::endl;
    }
}

和PHP裏的捕獲異常很相似。可是目前還不知道怎麼打印輸出e裏面的內容。spa

變量

Php :: Value類是對PHP變量zval的封裝,使用的時候能夠無縫在C++變量與PHP變量裏自動轉換。code

下面還列出一些特殊的PHP變量:orm

Php::Value 申明數組
Php::Object 申明對象
Php::GLOBALS PHP全局變量

示例:對象

// create a regular array
Php::Value array;
array[0] = "apple";
array[1] = "banana";
array[2] = "tomato";

// create empty object of type stdClass
Php::Object object;
object = Php::Object("DateTime", "now");

// methods can be called with the call() method
Php::out << object.call("format", "Y-m-d H:i:s") << std::endl;

// set a global PHP variable
Php::GLOBALS["a"] = 12345;

// global variables can be of any type
Php::GLOBALS["b"] = Php::Array({1,2,3,4});

常量

定義常量很簡單:blog

#include <phpcpp.h>

extern "C" {

    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("helloworld", "1.0");

        // add integer constants
        myExtension.add(Php::Constant("MY_CONSTANT_1", 1));
        myExtension.add(Php::Constant("MY_CONSTANT_2", 2));

        // floating point constants
        myExtension.add(Php::Constant("MY_CONSTANT_3", 3.1415927));
        myExtension.add(Php::Constant("MY_CONSTANT_4", 4.932843));

        // string constants
        myExtension.add(Php::Constant("MY_CONSTANT_5", "This is a constant value"));
        myExtension.add(Php::Constant("MY_CONSTANT_6", "Another constant value"));

        // null constants
        myExtension.add(Php::Constant("MY_CONSTANT_7", nullptr));

        // return the extension
        return myExtension;
    }
}

在PHP腳本中使用常量一樣簡單:

<?php
echo(MY_CONSTANT_1."\n");
echo(MY_CONSTANT_2."\n");
echo(MY_CONSTANT_3."\n");
echo(MY_CONSTANT_4."\n");
echo(MY_CONSTANT_5."\n");
echo(MY_CONSTANT_6."\n");
echo(MY_CONSTANT_7."\n");

類常量

#include <phpcpp.h>

class Dummy : public Php::Base
{
};

extern "C" {

    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("helloworld", "1.0");

        // create a class objects
        Php::Class<Dummy> dummy("Dummy");

        // 有不少種方式添加類常量,但實現效果同樣
        dummy.property("MY_CONSTANT_1", 1, Php::Const);
        dummy.property("MY_CONSTANT_2", "abcd", Php::Const);
        dummy.constant("MY_CONSTANT_3", "xyz");
        dummy.constant("MY_CONSTANT_4", 3.1415);
        dummy.add(Php::Constant("MY_CONSTANT_5", "constant string"));
        dummy.add(Php::Constant("MY_CONSTANT_5", true));

        // add the class to the extension
        myExtension.add(std::move(dummy));

        // return the extension
        return myExtension;
    }
}

運行時常量

若是要在運行時從C++代碼中找出用戶空間常量的值,或者想要知道是否認義了常量,能夠簡單地使用Php::constant()Php::defined()函數。

要在運行時定義常量,請使用Php::define()

#include <phpcpp.h>

void example_function()
{
    // check if a certain user space constant is defined
    if (Php::defined("USER_SPACE_CONSTANT"))
    {
        // retrieve the value of a constant
        Php::Value constant = Php::constant("ANOTHER_CONSTANT");

        // define other constants at runtime
        Php::define("DYNAMIC_CONSTANT", 12345);
    }
}

extern "C" {

    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("helloworld", "1.0");

        // add a function to the extension
        extension.add("example_function", example_function);

        // return the extension
        return myExtension;
    }
}

(未完待續~fhyblog)

相關文章
相關標籤/搜索