32
Yohei Sato 1 初めての PHP Extension id:yokkuns 洋平 PHP カンファレンス 2009

PHPカンファレンス[T-109]初めてのPHP Extension

Embed Size (px)

Citation preview

Page 1: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 1

初めての PHP Extension

id:yokkuns 里 洋平

PHP カンファレンス 2009

Page 2: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 2

アジェンダ● 自己紹介● PHP Extension とは● 引数と返り値● PHP の内部構造● PHP Extension の作成

Page 3: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 3

自己紹介● id:yokkuns● 名前 : 里 洋平● 職業 :Web エンジニア● 出身 : 種子島● 趣味 : プログラミングとかカラオケとか● 仕事は、 PHP では無く、 Perl

Page 4: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 4

PHP Extension とは?

Page 5: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 5

PHP Extension とは● PHP の拡張モジュール● C/C++ で書かれてる● 例) mysql 、 sockets 、 SimpleXML など

Page 6: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 6

何故 PHP Extension ?● 過去に C で書かれた既存のライブラリを流用したい

● 実行速度。 C なので、当然 PHP で書くより速い。

Page 7: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 7

引数と返り値

Page 8: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 8

Extension で行う処理● 引数の取得● データ作成処理● 返り値の出力

Page 9: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 9

データ型の問題● そのままでは、データのやりとりが出来ない。

$a$a = 100

Page 10: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 10

データ型の問題● そのままでは、データのやりとりが出来ない。

a

int a = 100

Page 11: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 11

そこで、 Zend API

ZendAPI

$a

$a = 100

long a

a == 100

Page 12: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 12

そこで、 Zend API

ZendAPI

$a

$a == 100

a

long a =100

Page 13: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 13

PHP の内部構造

Page 14: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 14

zval 構造体

zval構造体

value refcount type is_ref

$a = 100

Page 15: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 15

zval 構造体

zval構造体

value refcount type is_ref

$a = 100

type = IS_LONG

Page 16: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 16

zval 構造体

zval構造体

value refcount type is_ref

$a = 100

zvalue_value共用体

lval dval str ht objtype = IS_LONG

Page 17: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 17

zval 構造体

zval構造体

value refcount type is_ref

$a = 100

zvalue_value共用体

lval dval str ht obj

100

type = IS_LONG

Page 18: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 18

文字列の場合

zval構造体

value refcount type is_ref

$a = "PHP"

zvalue_value共用体

lval dval str ht obj

type = IS_STRING

str構造体

val len

PHP3

Page 19: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 19

PHP のデータタイプ● IS_NULL

● IS_LONG

● IS_DOUBLE

● IS_BOOL

● IS_ARRAY

● IS_OBJECT

● IS_STRING

● IS_RESOURCE

● IS_CONSTANT

● IS_CONSTANT_ARRAY

Page 20: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 20

PHP の配列● PHP の配列は、 C とかで言う配列とは別物● 順番付けられたマップ

Page 21: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 21

PHP の配列の構造

zval

type = IS_ARRAY

zvalue_value

a

b

c

zval

zval

zval

hashtable

Page 22: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 22

PHP のオブジェクトの構造

zval

type = IS_OBJECT

zvalue_value zend_object_value構造体

handle handlers zend_object_handlers

構造体

Page 23: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 23

zend_object_handlerszend_object_handlers

add_ref del_ref delete_obj clone_obj read_property write_property get_property_ptr get_property_zval_ptr get set has_property unsert_property get_properties get_method call_method get_constructor get_class_entry get_class_name compare_objects

Page 24: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 24

PHP Extension の作成

Page 25: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 25

PHP Extension 作成方法● 昔は、 ext_skel コマンド

– スケルトンを生成し、それを直接修正

● 今は、 CodeGen_PECL– XML ファイルから PECL 用のコードを生成

Page 26: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 26

CodeGen_PECL を使ってExtension を作成

Page 27: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 27

・・・と思ったんですが

Page 28: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 28

何と時間が無い!

Page 29: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 29

と、言うわけで続きは、次回やります!

Page 30: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 30

ご清聴ありがとうございました。

Page 31: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 31

参考サイト● PHP: Zend API: PHP のコアをハックする - Manual

– http://www.php.net/manual/ja/internals2.ze1.zendapi.php

● PHP 変数管理を理解する - CPA-LAB テクニカル

– http://www.cpa-lab.com/tech/032

● DSAS 開発者の部屋 :PHP Extension を作ろう第1回 - まずは Hello World

– http://dsas.blog.klab.org/archives/50777398.html

● DSAS 開発者の部屋 :PHP Extension を作ろう第2回 - 引数と返値

– http://dsas.blog.klab.org/archives/50782987.html

● DSAS 開発者の部屋 :PHP Extension を作ろう第3回 - クラスを作ろう

– http://dsas.blog.klab.org/archives/50903613.html

Page 32: PHPカンファレンス[T-109]初めてのPHP Extension

Yohei Sato 32

参考書籍● Extending and Embedding PHP

– http://alturl.com/tu93

● PHP5 徹底攻略エクスパート編

– http://alturl.com/nymh