2008年4月24日星期四

在 Ubuntu 7.10 開發 PHP5 Module (模組)

由於工作關係, 我需要開發一個 PHP5 Module. 我看過網上的文章和 PHP 手冊, 發覺大部份例子都是使用 ext_skel 來建立PHP Module project. 但是 ext_skel 只在 PHP5 源碼找到. 如果沒有下載和由 PHP5 源碼來建立 PHP5 系統的話, 在 Ubuntu 是找不到 ext_skel 的. 原來之前 Ubuntu 已經將 ext_skel 移除了, 現在它改為建議用 pecl-gen 來代替 ext_skel.

要使用 pecl-gen, 首先我們需要安裝 pear 和有關的 package:

php5-cli
php5-dev
php-pear

如果電腦可以上網, 我們可以使用 pear 來安裝 pecl-gen:

sudo pear install CodeGen_PECL

如果電腦不可以上網, 我們可以先找一台能夠上網的電腦到 pear 的網站下載以下檔案:

CodeGen package
CodeGen_PECL package

然後將檔案複製到不能上網的電腦上.

假設 CodeGen 和 CodeGen_PECL 的檔案為

CodeGen-1.0.5.tgz
CodeGen_PECL-1.1.2.tgz

我們可以用以下方法安裝:

sudo pear install CodeGen-1.0.5.tgz
sudo pear install CodeGen_PECL-1.1.2.tgz

安裝完成後, 我們可以輸入 pecl-gen 來檢正是否安裝成功.

要建立 PHP Module project, 我們可以跟 ext_skel 一樣:

pecl-gen --extname=[your module name]

例如 module name 是 hello_world, 則輸入:

pecl-gen --extname=hello_world

而該 module 的檔案會在 [your module name] 目錄內.

要 make PHP module, 我們可以輸入:

phpize
./configure
make
make test

安裝 module 則可以輸入:

sudo make install

然後建立一個 ini 檔案:

touch [your module name].ini

在 ini 檔案內加入:

extension=[your module name].so

之後複製 ini 檔案到 /etc/php5/cli/conf.d 目錄內:

sudo cp [your module name].ini /etc/php5/cli/conf.d

然後 restart WWW server 就可以開始使用 Module 了:

sudo /etc/init.d/apache2 restart

當然, 我們可以用 phpinfo() 來檢查 module 是否已經載入

2 則留言:

睡觉的眼皮 說...

HELP! Can you tell me what's wrong with following? Did I missed anything?

[root@S07 ext]# pecl-gen --force --extname=sample9 --proto=sample9.xml
Warning: Missing argument 2 for CodeGen_PECL_Element_Function::setProto(), called in /usr/share/pear/CodeGen/PECL/Command.php on line 189 and defined in /usr/share/pear/CodeGen/PECL/Element/Function.php on line 270
Notice: Undefined variable: extension in /usr/share/pear/CodeGen/PECL/Element/Function.php on line 274
PHP Fatal error: Call to a member function haveVersion() on a non-object in /usr/share/pear/CodeGen/PECL/Element/Function.php on line 274

tglcowcow 說...

I really don't know how to solve your problem.
I have visited PEAR site (http://pear.php.net/package/CodeGen_PECL/docs/latest/CodeGen_PECL/CodeGen_PECL_Element_Function.html#methodsetProto) and I guess the second argument $extension(object owning this function) is misssing during the project generation process.
Therefore, I suggest:
1. update CodeGen_PECL package to the latest version
2. check your XML file. You might have to read the CodeGen_PECL manual (for example: http://php-baustelle.de/CodeGen_PECL/manual.html)
I wish the above suggestions could help you.....good luck