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 ¶ms)
- {
- 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."\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)
来源: https://www.cnblogs.com/52fhy/p/9657346.html