[教學] 用 PHP-CPP 寫 PHP extension (2)

看板PHP作者 (Neisseria)時間8年前 (2016/12/07 11:39), 6年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
延續上一篇,我們會展示另一個範例 在這個範例中,我們會建立 PHP 物件 (object),並呼叫其方法 (method) 首先,實作 matrix 這個 toy library 在這裡,我們也是用 Rust 實作。同樣地,也要手動撰寫 header #ifndef _MATRIX_H_ #define _MATRIX_H_ #ifdef __cplusplus extern "C" { #endif void* matrix_new(size_t, size_t); double matrix_get_at(void*, size_t, size_t); void matrix_set_at(void*, double, size_t, size_t); void matrix_free(void*); #ifdef __cplusplus } #endif #endif // _MATRIX_H_ 為了簡化範例,我們沒有實作 matrix 的操作 僅建立 getter/setter,請板友包涵 同樣要下載 EmptyExtension 這個專案骨架,請板友自行完成。 接下來,實作 C++ 程式碼 #include <phpcpp.h> #include "matrix.h" class Matrix : public Php::Base { public: void* m; // Our matrix struct Matrix() = default; // Dummy constructor ~Matrix(); void __construct(Php::Parameters&); // PHP constructor Php::Value get_at(Php::Parameters&); void set_at(Php::Parameters&); }; Matrix::~Matrix() { matrix_free(this->m); } void Matrix::__construct(Php::Parameters &params) { this->m = matrix_new((int32_t)params[0], (int32_t)params[1]); } Php::Value Matrix::get_at(Php::Parameters &params) { return matrix_get_at(this->m, (int32_t)params[0], (int32_t)params[1]); } void Matrix::set_at(Php::Parameters &params) { matrix_set_at(this->m, (double)params[0], (int32_t)params[1], (int32_t)params[2]); } 要特別注意 constructor 的部分。由於 PHP 不能直接使用 C++ 的 constructor 要額外建立一個 __construct 方法,並在裡面實做 constructor 的程式碼 另外建立一個 dummy constructor 給 C++ 用 在我們這個專案中,m 是實際從 Rust 輸出的 struct。 一般常規是將其設為 private,這裡將其設為 public 因為在 PHP 層級,不會動到這個 struct 將其設為 public 有利於物件操作 e.g. matrix 相乘 同樣地,將其輸出到 PHP extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension extension("matrix", "1.0"); Php::Class<Matrix> matrix("Matrix"); matrix.method<&Matrix::__construct>("__construct", { Php::ByVal("row", Php::Type::Numeric), Php::ByVal("col", Php::Type::Numeric) }); matrix.method<&Matrix::get_at>("get_at", { Php::ByVal("row", Php::Type::Numeric), Php::ByVal("col", Php::Type::Numeric) }); matrix.method<&Matrix::set_at>("set_at", { Php::ByVal("value", Php::Type::Float), Php::ByVal("row", Php::Type::Numeric), Php::ByVal("col", Php::Type::Numeric) }); extension.add(std::move(matrix)); return extension; } } 最後,撰寫簡單的 PHP 程式測試此 PHP extension <?php // main.php $m = new Matrix(3, 3); echo $m->get_at(1, 1), "\n"; $m->set_at(99, 1, 1); echo $m->get_at(1, 1), "\n"; 若不想安裝 extension,也可從命令列呼叫 $ php -dextension=`pwd`/matrix.so main.php 這個範例到此大致上結束了 使用 PHP-CPP,仍然需要基本的 C++ 相關知識 而且,PHP-CPP 官網說明略為簡略,有時還是得去翻 PHP-CPP 的 header 如果使用 Zephir,可以用更接近 PHP 的語法來撰寫 那個方案較好,板友可再自行評估 分享給有需要的板友 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.36.80 ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1481081982.A.52A.html ※ 編輯: Neisseria (125.227.36.80), 12/07/2016 11:51:00 ※ 編輯: Neisseria (60.251.46.166), 12/24/2018 16:07:05
文章代碼(AID): #1OHuH-Kg (PHP)
文章代碼(AID): #1OHuH-Kg (PHP)