라이브러리
[PHP] FFI::typeof - FFICData의 FFICType을 가져옵니다.
PHP FFI::typeof
PHP 8.1 버전부터 `FFI` (Foreign Function Interface) 모듈이 추가되었습니다. `FFI` 모듈은 C 언어와 같은 다른 언어에서 작성된 함수를 PHP에서 호출할 수 있도록 해줍니다. `FFI::typeof` 함수는 C 언어에서 사용하는 타입을 PHP에서 사용하는 타입으로 변환해 줍니다.
# 예제
#hostingforum.kr
php
// C 언어에서 정의한 함수
extern "C" {
void print_int(int a);
void print_string(const char* a);
}
// PHP에서 FFI::new 함수를 사용하여 C 언어 함수를 호출
$ffi = FFI::cdef("
void print_int(int a);
void print_string(const char* a);
", "libexample.so");
// FFI::typeof 함수를 사용하여 C 언어 타입을 PHP 타입으로 변환
$ffi->print_int(123); // int 타입
$ffi->print_string("Hello, World!"); // const char* 타입
// FFI::typeof 함수를 사용하여 타입을 확인
echo FFI::typeof($ffi->print_int) . "
"; // int
echo FFI::typeof($ffi->print_string) . "
"; // string
# 사용법
1. `FFI::cdef` 함수를 사용하여 C 언어 함수를 PHP에서 호출할 수 있도록 정의합니다.
2. `FFI::new` 함수를 사용하여 C 언어 함수를 호출합니다.
3. `FFI::typeof` 함수를 사용하여 C 언어 타입을 PHP 타입으로 변환합니다.
# 예시 코드
#hostingforum.kr
php
// example.c
#include
void print_int(int a) {
printf("%d
", a);
}
void print_string(const char* a) {
printf("%s
", a);
}
// example.php
$ffi = FFI::cdef("
void print_int(int a);
void print_string(const char* a);
", "example.so");
$ffi->print_int(123);
$ffi->print_string("Hello, World!");
echo FFI::typeof($ffi->print_int) . "
"; // int
echo FFI::typeof($ffi->print_string) . "
"; // string
# 빌드
#hostingforum.kr
bash
gcc -shared -o example.so example.c
# 실행
#hostingforum.kr
bash
php example.php
# 결과
#hostingforum.kr
123
Hello, World!
int
string
# 참고
* [PHP FFI](https://www.php.net/manual/en/book.ffi.php)
* [FFI::cdef](https://www.php.net/manual/en/ffi.cdef.php)
* [FFI::new](https://www.php.net/manual/en/ffi.new.php)
* [FFI::typeof](https://www.php.net/manual/en/ffi.typeof.php)
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.