<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                **Example #1 從共享庫調用函數** ``` //?create?FFI?object,?loading?libc?and?exporting?function?printf() $ffi?=?FFI::cdef( ????"int?printf(const?char?*format,?...);",?//?this?is?a?regular?C?declaration ????"libc.so.6"); //?call?C's?printf() $ffi->printf("Hello?%s!\n",?"world"); ``` 以上例程會輸出: ~~~ Hello world! ~~~ **Example #2 調用函數,通過參數返回結構** ``` // create gettimeofday() binding $ffi = FFI::cdef(" typedef unsigned int time_t; typedef unsigned int suseconds_t; struct timeval { time_t tv_sec; suseconds_t tv_usec; }; struct timezone { int tz_minuteswest; int tz_dsttime; }; int gettimeofday(struct timeval *tv, struct timezone *tz); ", "libc.so.6"); // create C data structures $tv = $ffi->new("struct timeval"); $tz = $ffi->new("struct timezone"); // call C's gettimeofday() var_dump($ffi->gettimeofday(FFI::addr($tv), FFI::addr($tz))); // access field of C data structure var_dump($tv->tv_sec); // print the whole C data structure var_dump($tz); ``` 以上例程的輸出類似于: ~~~ int(0) int(1555946835) object(FFI\CData:struct timezone)#3 (2) { ["tz_minuteswest"]=> int(0) ["tz_dsttime"]=> int(0) } ~~~ **Example #3 訪問現有的C變量** ``` // create FFI object, loading libc and exporting errno variable $ffi = FFI::cdef( "int errno;", // this is a regular C declaration "libc.so.6"); // print C's errno var_dump($ffi->errno); ``` 以上例程會輸出: ~~~ int(0) ~~~ **Example #4 創建和修改C變量** ``` //?create?a?new?C?int?variable $x?=?FFI::new("int"); var_dump($x->cdata); //?simple?assignment $x->cdata?=?5; var_dump($x->cdata); //?compound?assignment $x->cdata?+=?2; var_dump($x->cdata); ``` 以上例程會輸出: ~~~ int(0) int(5) int(7) ~~~ **Example #5 使用C數組** ``` //?create?C?data?structure $a?=?FFI::new("long[1024]"); //?work?with?it?like?with?a?regular?PHP?array for?($i?=?0;?$i?<?count($a);?$i++)?{ ????$a[$i]?=?$i; } var_dump($a[25]); $sum?=?0; foreach?($a?as?$n)?{ ????$sum?+=?$n; } var_dump($sum); var_dump(count($a)); var_dump(FFI::sizeof($a)); ``` 以上例程會輸出: ~~~ int(25) int(523776) int(1024) int(8192) ~~~ 可以將PHP閉包分配給函數指針類型的本機變量,也可以將其作為函數參數傳遞 ``` $zend?=?FFI::cdef(" ????typedef?int?(*zend_write_func_t)(const?char?*str,?size_t?str_length); ????extern?zend_write_func_t?zend_write; "); ? echo?"Hello?World?1!\n"; ? $orig_zend_write?=?clone?$zend->zend_write; $zend->zend_write?=?function($str,?$len)?{ ????global?$orig_zend_write; ????$orig_zend_write("{\n\t",?3); ????$ret?=?$orig_zend_write($str,?$len); ????$orig_zend_write("}\n",?2); ????return?$ret; }; echo?"Hello?World?2!\n"; $zend->zend_write?=?$orig_zend_write; echo?"Hello?World?3!\n"; ``` 以上例程會輸出: ~~~ Hello World 1! { Hello World 2! } Hello World 3! ~~~ >[warning]盡管這樣做是有效的,但是并不是所有libffi平臺都支持這種功能,這種功能效率不高,并且在請求結束時泄漏資源 因此,建議盡量減少使用PHP回調 ## **一個完整的PHP/FFI/預加載示例** php.ini ~~~ ffi.enable=preload opcache.preload=preload.php ~~~ preload.php ``` FFI::load(__DIR__?.?"/dummy.h"); opcache_compile_file(__DIR__?.?"/dummy.php"); ``` dummy.h ~~~ #define FFI_SCOPE "DUMMY" #define FFI_LIB "libc.so.6" int printf(const char *format, ...); ~~~ dummy.php ``` final?class?Dummy?{ ????private?static?$ffi?=?null; ????function?__construct()?{ ????????if?(is_null(self::$ffi))?{ ????????????self::$ffi?=?FFI::scope("DUMMY"); ????????} ????} ????function?printf($format,?...$args)?{ ???????return?(int)self::$ffi->printf($format,?...$args); ????} } ``` test.php ``` $d?=?new?Dummy(); $d->printf("Hello?%s!\n",?"world"); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看