首页 技术 正文
技术 2022年11月8日
0 收藏 907 点赞 1,637 浏览 1351 个字

PHP-CPP是一个用于开发PHP扩展的C++库。本节讲解如何在C++中调用PHP函数。

调用PHP函数

调用普通函数

// call a function from user space
Php::Value data = Php::call("some_function", "some_parameter");

调用类里方法:

// create an object (this will also call __construct())
Php::Object time("DateTime", "now");// call a method on the datetime object
Php::out << time.call("format", "Y-m-d H:i:s") << std::endl;

下面通过一个综合示例来说明:

/**
* User: 公众号: 飞鸿影的博客(fhyblog)
* Date: 2018/7
*/#include <phpcpp.h>void test_call(Php::Parameters &params){ Php::Value v; //调用普通方法
v = Php::call("md5", "test");
Php::out << v << std::endl; //实例化PHP类并调用类里的方法
Php::Object time("Datetime", "now");
v = time.call("format", "Y-m-d H:i:s");
Php::out << v << std::endl; //形参是PHP类,调用其方法
Php::Value datetime = params[0];
v = datetime.call("format", "Y-m-d");
Php::out << v << std::endl; //形参是匿名函数
Php::Value callback = params[1];
callback("param1", "param2"); //给已知对象的成员方法起别名
Php::Array format_alias({datetime, "format"});
Php::out << format_alias("Y-m-d H") << std::endl; //普通方法别名
Php::Value time_alias("time");
Php::out << time_alias() << std::endl;
}extern "C" {
PHPCPP_EXPORT void *get_module() {
static Php::Extension extension("helloworld", "1.0.0");
extension.add<test_call>("test_call"); return extension;
}
}

我们使用test.php进行测试:

<?php
test_call(new DateTime(), function($a, $b){
var_dump($a, $b);
});

输出:

098f6bcd4621d373cade4e832627b4f6
2018-07-15 09:43:23
2018-07-15
string(6) "param1"
string(6) "param2"
2018-07-15 09
1531619003

(未完待续)

想第一时间获取最新动态,欢迎关注关注飞鸿影的博客(fhyblog),不定期为您呈现技术干货。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,001
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850