Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
20181215 php-srcで遊ぶよ
Search
Matsumoto Kazutaka
December 15, 2018
Programming
2
1.1k
20181215 php-srcで遊ぶよ
php-srcで遊んでみた
php conference 2018の資料です
Matsumoto Kazutaka
December 15, 2018
Tweet
Share
More Decks by Matsumoto Kazutaka
See All by Matsumoto Kazutaka
GPTsより精度の高いRAGシステムの構築
mkazutaka
26
12k
LLMと連携したブログ書くエディタを作ってみた
mkazutaka
2
400
社内情報検索システムで用いられるRAGの4つの実装方法
mkazutaka
14
9.1k
ChatGPTを使ったSlackbotの実装いろいろ紹介
mkazutaka
2
2.6k
Development of Mercari's web in 2019
mkazutaka
0
1k
メルカリ Backendエンジニアの日常 ~Backend Engineer Drink Meetup #1~
mkazutaka
0
2.1k
Other Decks in Programming
See All in Programming
Contribute to Comunities | React Tokyo Meetup #4 LT
sasagar
0
570
note の Elasticsearch 更新系を支える技術
tchov
0
190
Compose Hot Reload is here, stop re-launching your apps! (Android Makers 2025)
zsmb
1
580
Exit 8 for SwiftUI
ojun9
0
150
Road to RubyKaigi: Making Tinny Chiptunes with Ruby
makicamel
4
490
カオスに立ち向かう小規模チームの装備の選択〜フルスタックTSという装備の強み _ 弱み〜/Choosing equipment for a small team facing chaos ~ Strengths and weaknesses of full-stack TS~
bitkey
1
120
AI時代の開発者評価について
ayumuu
0
220
ComposeでWebアプリを作る技術
tbsten
0
120
Bedrock×MCPで社内ブログ執筆文化を育てたい!
har1101
6
1.2k
On-the-fly Suggestions of Rewriting Method Deprecations
ohbarye
1
3.9k
Making TCPSocket.new "Happy"!
coe401_
1
2.3k
The Evolution of the CRuby Build System
kateinoigakukun
1
740
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
54
5.5k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.5k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Code Review Best Practice
trishagee
67
18k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.3k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
We Have a Design System, Now What?
morganepeng
52
7.5k
Done Done
chrislema
184
16k
Visualization
eitanlees
146
16k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Transcript
php-src で遊ぶよ Github:@mkazutaka Twitter:@makazutaka Name: Kazutaka Matsumoto
who am i? 名前: Kazutaka Matsumoto 所属: 株式会社メルカリ 職種: バックエンドエンジニア(PHP
or Go) PHP 歴: 8 ヶ月(2018 新卒)
最近書いたよー Microservices にジョインするには知らない技術が多すぎたので 一通り触ってみた話 https://tech.mercari.com/entry/2018/12/02/160455
タイトル php-src で遊ぶよ
PHP 初心者のジレンマ PHP で開発するからにはphp-src のことを知りたい php-src を知らなくてもPHP はかける ex) Laravel
を読むのにphp-src は必要ない
ジレンマの難しいところ php-src は自分の時間で読む必要がある
さらにphp-src を読むハードルの高さも相まる
さらに プログラミング言語読んでも ・お金も儲からないし ・仕事に直接役立つわけでもないし ・そもそも効果わからんし
php-src で遊ぼうというタイトルで 申し込みしました
反省
今回のスライド pros) 浅いところまでなので聞き手が理解できる cons) 知識が浅い
Goal this section php-src で遊ぶ 副産物: php-src について知れる
Question php-src の中身を見たことある人 どれくらいいますか?
Question 拡張ライブラリを書いたことある人 どれくらいいます?
はじまり
はじめになにをやるか
手元にソースコードを置く
手元にソースコードを置く $ git clone
[email protected]
:php/php-src.git $ cd php-src $ git
checkout -b develop
ビルドする
Docke le $ vim Dockerfile // Dockerfile FROM ubuntu:18.10 RUN
apt update RUN apt upgrade -y RUN apt install -y build-essential autoconf automake \ libtool bison re2c libxml2-dev libsqlite3-dev ENV DIST_DIR=/tmp/php-src RUN mkdir -p $DIST_DIR WORKDIR $DIST_DIR
docker build And php-src build php-src のビルド ## docker run
-v $PWD:/tmp/php-src -it php-src ./buildconf でも可能 > ./buildconf > ./configure --enable-debug --enable-maintainer-zts \ --with-readline \ --disable-all > grep "cpu cores" /proc/cpuinfo #4 > make -j4 $ docker build . -t php-src $ docker run -v $PWD:/tmp/php-src -it php-src bash root@# ls CODING_STANDARDS Makefile.fragments README.NEW-OUTPUT-API README.W CONTRIBUTING.md Makefile.gcov README.PARAMETER_PARSING_API README.i ...
実行 root@# sapi/cli/php -m [PHP Modules] Core ... [Zend Modules]
root@# sapi/cli/php -a Interactive mode enabled php > echo 'Hello World'; Hello World
PHP overview ref: https://entwickler.de/wp-content/uploads/2013/06/wpm_2013_06.pdf
PHP overview overview ref: https://www.slideshare.net/do_aki/php-sapi-zendengine3
SAPI The Server Application Programming Interface PHP にアクセスするためのインターフェース エンドポイントとしての役割を持つ CLI
やCGI はSAPI を経由してPHPCore 等にアクセス
ZendEngine コンパイラやVM といった機能を提供 上記を実現するための機能を提供 ZendEngine はPHP Core やSAPI から独立して提供されているはず メモリ管理のために構造体などを提供
詳しくは@do_aki さんのPHP と SAPI と ZendEngine3 がおすすめ http://d.hatena.ne.jp/do_aki/20180313/1520937886
PHP Core ZendEngine と役割は似てる ネットワークの管理やコマンドラインからのarugment の読むこむ 関数としてはfopen_function 等がある
php-src の主なせディレクトリ構成 . ├── Zend // ZendEngine 本体のディレクトリ ├── ext
// 拡張ライブラリや動的ライブラリが格納 ├── main // SAPI やextension の共通メソッドが置かれている ...
拡張モジュール作ってみる 拡張モジュールの雛形は、ext_skel コマンドで作成できる $ ./ext_skel.php --ext 'hello_world'; cd hello_world $
ls config.m4 config.w32 hello_world.c php_hello_world.h tests $ ./buildconf $ ./configure --enable-debug \ --enable-maintainer-zts \ --with-readline \ --enable-hello_world --disable-all $ make -j4 $ sapi/cli/php -m [PHP Modules] ... hello_world ...
書いてみる 関数の定義 // ext/hello_world/hello_world.c PHP_FUNCTION(say_hello_world) { ZEND_PARSE_PARAMETERS_NONE(); php_printf("Hello World"); }
引数の定義 // ext/hello_world/hello_world.c ZEND_BEGIN_ARG_INFO(arginfo_say_hello_world, 0) ZEND_END_ARG_INFO() module functions に追加 //
ext/hello_world/hello_world.c static const zend_function_entry hello_world_functions[] = { PHP_FE(say_hello_world, arginfo_say_hello_world) PHP_FE_END };
実行してみる ## ビルドし直す $ ./configure --enable-debug \ --enable-maintainer-zts \ --with-readline
\ --enable-hello_world --disable-all $ make -j4 $ grep "cpu cores" /proc/cpuinfo #4 $ sapi/cli/php -a php > say_hello_world(); Hello World
関数に引数を使う PHP_FUNCTION(say_with_world) { char *s; size_t s_len; zend_string *r; ZEND_PARSE_PARAMETERS_START(1,
1) Z_PARAM_STRING(s, s_len) ZEND_PARSE_PARAMETERS_END(); r = strpprintf(0, "%s world", s); RETURN_STR(r); } ZEND_BEGIN_ARG_INFO(arginfo_say_with_world, 0) ZEND_ARG_INFO(0, str) ZEND_END_ARG_INFO()
static const zend_function_entry hello_world_functions[] = { ... PHP_FE(say_with_world, arginfo_say_with_world) ...
}; 実行 php > print_r(say_with_world('hoge')); hoge world
まとめ php-src で遊んでみました それに伴い、PHP Core とZend Engine に対して簡単に解説をした また実際に、拡張ライブラリを実装を紹介した
時間があれば
RFC(Request for Comments) 技術仕様の提案や決定が書かれているページがある https://wiki.php.net/rfc Github のPR のリンクが書かれている
RFC: array_key_ rst().... 読める気がする RFC: https://wiki.php.net/rfc/array_key_ rst_last PR: https://github.com/php/php-src/pull/3256
まとめ php-src で遊ぶ php-src の概要を知る必要がある ソースコードの読み方を知る必要がある
終わり
参考資料 https://wiki.php.net/rfc http://php.net/manual/ja/internals2.ze1.zendapi.php https://qiita.com/7968/items/2aa487e2e515dba2fd03 https://thinkit.co.jp/free/article/0705/7/1/