zoukankan      html  css  js  c++  java
  • PHP-CPP开发扩展(六)

    PHP-CPP是一个用于开发PHP扩展的C++库。本节讲解在C++中PHP异常、变量、常量的实现相关知识。

    异常

    PHP和C++都支持异常,而PHP-CPP库这两种语言之间的异常处理是完全透明的。你在C++中抛出的异常会自动传递给PHP脚本,并且你的C++代码可以捕获PHP脚本抛出的异常,就像它是普通的C++异常一样。

    遗憾的是,PHP-CPP目前仅支持PHP标准异常Exception,还不支持自定义异常。

    抛出异常

    示例:

    #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里面的内容。

    变量

    Php :: Value类是对PHP变量zval的封装,使用的时候可以无缝在C++变量与PHP变量里自动转换。

    下面还列出一些特殊的PHP变量:

    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});
    

    常量

    定义常量很简单:

    #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."
    ");
    echo(MY_CONSTANT_2."
    ");
    echo(MY_CONSTANT_3."
    ");
    echo(MY_CONSTANT_4."
    ");
    echo(MY_CONSTANT_5."
    ");
    echo(MY_CONSTANT_6."
    ");
    echo(MY_CONSTANT_7."
    ");
    

    类常量

    #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)

  • 相关阅读:
    TongJI Online Judge预赛(3): Game
    堆栈小应用:配对
    在.net中使用Udp协议创建简单的聊天程序
    TongJI Online Judge预赛(2): LOVE LETTER
    全排列问题之递归求解
    如何打造RSS阅读器
    Html 常用标志总结
    实现页面的分帧显示
    每天OnlineJudge之 “数素数”
    文本编辑器中,如何设计 撤销/重复栈
  • 原文地址:https://www.cnblogs.com/52fhy/p/9657346.html
Copyright © 2011-2022 走看看