- PHPで開発を行っている中級者向け
普段はApache2等のmodule上で実行されるPHPですが、Node.jsで使われているlibuvをPHPでも使えるようにしたPHP拡張
php-uv を使う事により軽量なイベント駆動やネットワークアプリケーションを簡単に書けるようになります。
本発表ではlibuvの簡単な概要説明からphp-uvを使ったEvent-driven, Nonblocking / IOを使ったプログラミング例の紹介、
| <?php | |
| /** | |
| * RpbErrorResp | |
| * | |
| * @message RpbErrorResp | |
| * | |
| * -*- magic properties -*- | |
| * | |
| * @property string $errmsg | |
| * @property int $errcode |
| var http = require("http"); | |
| http.createServer(function(request, response) { | |
| response.writeHead(200, {"Content-Type": "text/plain"}); | |
| response.write("Hello World"); | |
| response.end(); | |
| }).listen(8888,"::1"); |
| <?php | |
| //https://github.com/chobie/php-uv | |
| $loop = uv_default_loop(); | |
| $queue = uv_queue_work($loop, function(){ | |
| var_dump("[queue]"); | |
| }, function(){ | |
| var_dump("[finished]"); | |
| }); |
| <?php | |
| $server = uv_tcp_init(); | |
| uv_tcp_bind($server, "0.0.0.0",5150); | |
| uv_listen($server,1000,function($server){ | |
| $client = uv_tcp_init(); | |
| uv_accept($server,$client); | |
| uv_read_start($client, function($buffer, $client){ | |
| uv_write($client,"Hello\n",function($status, $client){ |
| <?php | |
| namespace Pinatra; | |
| class Application | |
| { | |
| protected static $instance; | |
| public $routes = array(); | |
| public static function getInstnace() | |
| { |
| diff --git a/config.m4 b/config.m4 | |
| index 51a1cad..f1af506 100755 | |
| --- a/config.m4 | |
| +++ b/config.m4 | |
| @@ -55,5 +55,5 @@ if test "$PHP_REDIS" != "no"; then | |
| dnl | |
| dnl PHP_SUBST(REDIS_SHARED_LIBADD) | |
| - PHP_NEW_EXTENSION(redis, redis.c library.c redis_session.c redis_array.c redis_array_impl.c igbinary/igbinary.c igbinary/hash_si.c igbinary/hash_function.c, $ext_shared) | |
| + PHP_NEW_EXTENSION(redis, redis.c g_fmt.c library.c redis_session.c redis_array.c redis_array_impl.c igbinary/igbinary.c igbinary/hash_si.c igbinary/hash_function.c, $ext_shared) |
| <?php | |
| $mruby = new MRuby(); | |
| function niyoniyo(MrubyObject $niyo) | |
| { | |
| var_dump($niyo->say); | |
| } | |
| $mruby->run(<<<'EOH' | |
| require 'php' |
| <?php | |
| class CustomRender extends Sundown\Render\HTML | |
| { | |
| public function normaltext($paragraph) | |
| { | |
| return nl2br($paragraph); | |
| } | |
| } | |
| $render = new CustomRender(); |