mirror of
https://github.com/php-win-ext/phpy.git
synced 2026-03-24 17:02:15 +01:00
1.4 KiB
1.4 KiB
内存拷贝
Python 调用 PHP 函数/方法时,参数、返回值将可能存在内存拷贝,在编写性能敏感的程序时需要关注内存复制的开销。
参数
- 整型、布尔型、浮点型、空值
None始终为值传递 - 对象、资源、引用,始终为引用传递,不会复制内存
- 字符串、数组,将进行递归深拷贝转为原生类型
arg1 = 1234
arg2 = 1234.5678
arg3 = True
arg4 = phpy.Object('stdClass')
arg5 = phpy.Reference()
arg6 = phpy.call('fopen', "php://input", "r")
arg7 = {'hello' : 'world'}
arg8 = "hello world"
arg9 = [1, 2, 3, 4, 5]
phpy.call('test', arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
arg1、arg2、arg3将转为PHP中的整型、浮点型、布尔型,直接复制数值arg4、arg5、arg6将直接传递引用到PHP中,不会产生内存拷贝arg6、arg7、arg8将遍历、深度内存拷贝,并转为PHP的array、string
返回值
- 整型、布尔型、浮点型、空值(
None和null)将转为Python的原生类型 - 其他从
Python中返回的内容全部为PyObject类型 - 也可以在
PHP代码中使用PyObject对象类型,返回Python原生类型实现透明转发传递
避免内存复制
使用 phpy.String、phpy.Array 对象,调用 PHP 函数时将不会拷贝内存。