PHPcpp 变量和类型

    用PHPCPP来开发PHP扩展是非常容易的,最主要的就是变量类型和PHP中的变量类型一毛一样,最重要的是写法也是一毛一样。

    在PHP中,变量默认是没有类型的,我们赋给他整数,他就是整形,赋给他字符串他就是string,也就是说PHP中的变量类型是随着值来定义的。PHPCPP在这里也是做了很大的优化,实现了类型随值的类型来定义。

1Php::Value value1 = 1234; 2Php::Value value2 = "this is a string"; 3Php::Value value3 = std::string("another string"); 4Php::Value value4 = nullptr; 5Php::Value value5 = 123.45; 6Php::Value value6 = true;

    其中Php::Value是PHPCPP定义一种类型,这个类型会进行隐式转换,就如同我们在PHP中用到的变量。

1void myFunction(const Php::Value &value) 2{ 3 int value1 = value;//这里的value就被隐式转换了,不然string怎么可能赋给int,C++中是绝对不允许的 4 std::string value2 = value;//这里又给转换成了string 5 double value3 = value; 6 bool value4 = value; 7}

    PHPCPP中Php::Value是和PHP的变量类型对应的,所以你会定义PHP变量,很容易就能接受PHPCPP的变量定义方式。

PHPCPP中数组的定义,这里就不用过多介绍了,因为你是一个PHPER,所以你了解下面的含义吧。

1Php::Value array; 2array[0] = "apple"; 3array[1] = "banana"; 4array[2] = "tomato"; 5 6// an initializer list can be used to create a filled array 7Php::Value filled({ "a", "b", "c", "d"}); 8 9// you can cast an array to a vector, template parameter can be 10// any type that a Value object is compatible with (string, int, etc) 11std::vector<std::string> fruit = array; 12 13// create an associative array 14Php::Value assoc; 15assoc["apple"] = "green"; 16assoc["banana"] = "yellow"; 17assoc["tomato"] = "green"; 18 19// the variables in an array do not all have to be of the same type 20Php::Value assoc2; 21assoc2["x"] = "info@example.com"; 22assoc2["y"] = nullptr; 23assoc2["z"] = 123; 24 25// nested arrays are possible too 26Php::Value assoc2; 27assoc2["x"] = "info@example.com"; 28assoc2["y"] = nullptr; 29assoc2["z"][0] = "a"; 30assoc2["z"][1] = "b"; 31assoc2["z"][2] = "c";

    只需要记住,在PHPCPP中定义变量给PHP中交互就定义为PHP::VALUE就可以了。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java常用类(2)

三、时间处理相关类Date类:计算机世界把1970年1月1号定为基准时间,每个度量单位是毫秒(1秒的千分之一),用long类型的变量表示时间。Date分配Date对象并初始化对象,以表示自从标准基准时间(称为“历元”(epoch),即1970年1月1日08:00:00GMT)以来的指定毫秒数。示例:packagecn.tanjian

[Dart]Dart语言之旅<二>:变量

变量以下是创建变量并为其分配值的示例:varname'Bob';变量是引用。名为name的变量包含对值为“Bob”的String类型的对象的引用。默认值未初始化的变量的初始值为null。即使是数字类型的变量,初始值也为null,因为数字也是对象。intlineCount;assert(lineCountnull)

PHPcpp 变量和类型 - HelloWorld